使一个活动扩展ListActivity扩展BaseScreen

时间:2012-09-07 10:09:04

标签: android android-listview

我有两项活动。 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;
    }

我需要以下解决方法之一:

  • Make ScreenTwo扩展BaseScreen
  • “麻烦”代码行的替代方案
  • 覆盖后退按钮以开始新活动。

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)

2 个答案:

答案 0 :(得分:0)

如果您使用@android:id/list作为ListView的ID,请使用:

ListView listView = (ListView) findViewByID(android.R.id.list);

否则使用:

ListView listView = (ListView) findViewByID(R.id.listView); // the id of your listView given in xml file.

通过这种方式,您无需扩展ListActivity

答案 1 :(得分:0)

Make ScreenTwo extend BaseScreen

只有你需要改变的事情是:

XML中的

删除ListView的id = "@android:id/list"并使其类似  id = "@+id/yourListView"

并在Activity中而不是使用

this.mainListView = getListView();

使用

ListView yourListView = (ListView) findViewById(R.id.yourListView);

这些是您需要改变的唯一事项。如果你在改变它们后没有得到你想要的东西。那是因为其他一些原因,而不是继承问题

或者根据你的第3点使用它:

@Override
public void onBackPressed() {

   Intent intent = new Intent(CurrentActivity.this, ScreenThree.class);
   startActivity(setIntent);
}