挂起RuntimeException异常当我单击一个按钮,该按钮以新的布局打开一个新的类时

问题描述

| 我的应用程序最近开始抛出\“ Suspended(RuntimeException异常)\”,我认为我可能不小心弄乱了一些代码,但我无法查明问题所在。 单击早餐按钮时,这是我的日志聊天。   04-14 17:54:15.941:错误/ AndroidRuntime(6099):致命异常:主   04-14 17:54:15.941:错误/ AndroidRuntime(6099):java.lang.RuntimeException:无法启动活动ComponentInfo {com.merch.dine / com.merch.dine.myMenu}:java.lang.classCastException:android .widget.ImageButton   04-14 17:54:15.941:错误/ AndroidRuntime(6099):位于android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2737)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2753)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):位于android.app.ActivityThread.access $ 2500(ActivityThread.java:129)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):位于android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2107)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):位于android.os.Handler.dispatchMessage(Handler.java:99)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):位于android.os.Looper.loop(Looper.java:143)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):位于android.app.ActivityThread.main(ActivityThread.java:4701)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):位于java.lang.reflect.Method.invokeNative(本机方法)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):at java.lang.reflect.Method.invoke(Method.java:521)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):在dalvik.system.NativeStart.main(本机方法)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):原因:java.lang.classCastException:android.widget.ImageButton   04-14 17:54:15.941:错误/ AndroidRuntime(6099):位于com.merch.dine.myMenu.onCreate(myMenu.java:36)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)   04-14 17:54:15.941:错误/ AndroidRuntime(6099):位于android.app.ActivityThread.performlaunchActivity(ActivityThread.java:2701) 这是线程上的调试信息   线程[<1> main](已暂停(RuntimeException异常))       ActivityThread.performlaunchActivity(ActivityThread $ ActivityRecord,Intent)行:2659       ActivityThread.handleLaunchActivity(ActivityThread $ ActivityRecord,Intent)行:2753       ActivityThread.access $ 2500(ActivityThread,ActivityThread $ ActivityRecord,Intent)行:129       ActivityThread $ H.handleMessage(Message)行:2107       ActivityThread $ H(Handler).dispatchMessage(Message)行:99       Looper.loop()行:143       ActivityThread.main(String [])行:4701       Method.invokeNative(Object,Object [],Class,Class [],Class,int,boolean)行:不可用[本机方法]       Method.invoke(Object,Object ...)第521行       ZygoteInit $ MethodAndArgsCaller.run()行:868       ZygoteInit.main(String [])行:626       NativeStart.main(String [])行:不可用[本机方法] 这是我的带有导致崩溃的按钮的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);
}