挂起(异常RuntimeException)当我点击一个按钮,打开一个新的类与新的布局



我的应用程序最近开始抛出"Suspended (exception RuntimeException)"我想我可能不小心弄乱了一些代码,但我不能指出问题所在。

这是我点击早餐按钮时的日志聊天记录。

04-14 17:54:15.941: ERROR/AndroidRuntime(6099): FATAL EXCEPTION: main
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): java.lang.RuntimeException: Unable to start activity ComponentInfo{com. merchant .dine/com. merchant .dine. com. com.myMenu}: java.lang.ClassCastException: android.widget.ImageButton
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2737)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at android.app.ActivityThread.access$2500(ActivityThread.java:129)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2107)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at android.os. loop .loop(loop .java:143)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at android.app.ActivityThread.main(ActivityThread.java:4701)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at java.lang.reflect.Method。invokeNative(本地方法)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at java.lang.reflect.Method.invoke(Method.java:521)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
[04-14] 17:54:15.941: ERROR/AndroidRuntime(6099): at dalvik.system. native . art. exe主要(本地方法)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): cause by: java.lang.ClassCastException: android.widget.ImageButton
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at com. merchant .dine. mymenu . oncreate (myMenu.java:36)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2701)

下面是线程 的调试信息

Thread [<1> main] (suspend (exception RuntimeException))
ActivityThread。performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2659
ActivityThread。handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2753
ActivityThread。访问$2500(ActivityThread, ActivityThread$ActivityRecord, Intent)行:129
ActivityThread$H.handleMessage(Message) line: 2107
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
loop .loop() line: 143
ActivityThread.main(String[]) line: 4701
方法。invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
方法。调用(对象,对象…)行:521
ZygoteInit$MethodAndArgsCaller.run() line: 868
ZygoteInit.main(String[]) line: 626
NativeStart.main(String[]) line: not available [native method]

这是我的Java文件,带有导致崩溃的按钮

public class EnglishOne extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //FIXED LANDSCAPE
        setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
        //FULLSCREEN
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.english1);
        //button breakfast
        Button bBreakfast1 = (Button) findViewById(R.id.breakfast1);
        bBreakfast1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View V) {
                startActivity(new Intent("com.merch.dine.BREAKFASTONE"));
            }
        });
    }
}

这是按钮应该以空白xml布局开始的活动

public class breakfastone extends Activity {
    public void OnCreate(Bundle breakfastone) {
        super.onCreate(breakfastone);
        setContentView(R.layout.breakfast1);
    }
}

没关系。我使用XML设置onClick函数,然后编写java文件,以便在调用该函数时更改内容视图,从而避免了这个问题。

Xml按钮特定代码;

<ImageButton android:id="@+id/button1"
    android:layout_height="160dip"
    android:layout_width="285dip"
    android:onClick="switchLayer"
    android:src="@drawable/icon"
    android:layout_x="285dip"
    android:layout_y="481dip">
</ImageButton>

Java Button特定代码;

public void switchLayer(View view) {
    // Button1 switches layout
    setContentView(R.layout.layer2);
}

最新更新