如何解决挂起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);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。