Suspended (exception RuntimeException) When I click a button that opens a new class with a new layout
My app recently started throwing a "Suspended (exception RuntimeException)" I think I might have accidentally messed with some code but I cant pinpoint the problem.
This is my log chat when I click on the breakfast button.
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.merch.dine/com.merch.dine.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.Looper.loop(Looper.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(Native Method) 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.NativeStart.main(Native Method) 04-14 17:54:15.941: ERROR/AndroidRuntime(6099): Caused by: java.lang.ClassCastException: android.widget.ImageButton 04-14 17:54:15.941: ERROR/AndroidRuntime(6099): at com.merch.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)
Here is the debug info on the thread
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2659 ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2753 ActivityThread.access$2500(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 129 ActivityThread$H.handleMessage(Message) line: 2107 ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper.loop() line: 143 ActivityThread.main(String[]) line: 4701 Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] Method.invoke(Object, Object...) line: 521 ZygoteInit$MethodAndArgsCaller.run() line: 868 ZygoteInit.main(String[]) line: 626 NativeStart.main(String[]) line: not available [native method]
here is my java file with the button that causes the crash
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"));
}
});
}
}
This is the activity that the button should start with a blank xml layout
p开发者_开发问答ublic class breakfastone extends Activity {
public void OnCreate(Bundle breakfastone) {
super.onCreate(breakfastone);
setContentView(R.layout.breakfast1);
}
}
Never mind. I circumvented my problem by using XML to set the onClick
function and then coded my java file to change the content view when the function is called.
Xml button specific code;
<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 specific code;
public void switchLayer(View view) {
// Button1 switches layout
setContentView(R.layout.layer2);
}
精彩评论