我有两个活动。ScreenOne.java 和 ScreenTwo.java。
ScreenOne 扩展 BaseScreen ScreenTwo 扩展 ListActivity
我希望 ScreenTwo 扩展 BaseScreen 作为后退按钮事件使应用程序崩溃。
this.mainListView = getListView();
如果我更改扩展名,这是唯一会引发错误的行。
错误是:
The method getListView() is undefined for the type ScreenTwo.
我的后退按钮代码:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Intent intent = new Intent(getBaseContext(), ScreenThree.class);
startActivity(intent);
}
return true;
}
我需要以下解决方法之一:
- 使 ScreenTwo 扩展 BaseScreen
- “麻烦”代码行的替代方案
- 覆盖 Back 按钮以开始一个新活动。
LogCat 输出:
09-07 11:30:13.419: E/AndroidRuntime(2845): FATAL EXCEPTION: main
09-07 11:30:13.419: E/AndroidRuntime(2845): java.lang.ClassCastException: com.example.Droid.Screens.ScreenTwo
09-07 11:30:13.419: E/AndroidRuntime(2845): at com.example.Droid.Services.ScreenService.getCurrentScreen(ScreenService.java:226)
09-07 11:30:13.419: E/AndroidRuntime(2845): at com.example.Droid.Screens.BaseScreen.processKeyDown(BaseScreen.java:310)
09-07 11:30:13.419: E/AndroidRuntime(2845): at com.example.Droid.Main.onKeyDown(Main.java:183)
09-07 11:30:13.419: E/AndroidRuntime(2845): at android.view.KeyEvent.dispatch(KeyEvent.java:1256)
09-07 11:30:13.419: E/AndroidRuntime(2845): at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
09-07 11:30:13.419: E/AndroidRuntime(2845): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1709)
09-07 11:30:13.419: E/AndroidRuntime(2845): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2628)
09-07 11:30:13.419: E/AndroidRuntime(2845): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2603)
09-07 11:30:13.419: E/AndroidRuntime(2845): at android.view.ViewRoot.handleMessage(ViewRoot.java:1875)
09-07 11:30:13.419: E/AndroidRuntime(2845): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 11:30:13.419: E/AndroidRuntime(2845): at android.os.Looper.loop(Looper.java:123)
09-07 11:30:13.419: E/AndroidRuntime(2845): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-07 11:30:13.419: E/AndroidRuntime(2845): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 11:30:13.419: E/AndroidRuntime(2845): at java.lang.reflect.Method.invoke(Method.java:507)
09-07 11:30:13.419: E/AndroidRuntime(2845): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-07 11:30:13.419: E/AndroidRuntime(2845): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-07 11:30:13.419: E/AndroidRuntime(2845): at dalvik.system.NativeStart.main(Native Method)