启动意图会导致ClassCastException

时间:2012-09-10 18:19:09

标签: android actionbarsherlock

我已经使用ActionBarSherlock成功设置了一个ActionBar,它设置了当我选择一个选项时开始的意图。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_collection:
            startActivity(new Intent(this, Collection.class));
            return true;
        case R.id.menu_wishlist:
            startActivity(new Intent(this, Wishlist.class));
            return true;
    }

    return super.onOptionsItemSelected(item);
}

问题在于,当这个意图开始时,我得到了这个错误:

09-10 20:30:15.496: E/AndroidRuntime(334): FATAL EXCEPTION: main
09-10 20:30:15.496: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mypackage.thing/com.mypackage.thing.Wishlist}: java.lang.ClassCastException: com.mypackage.thing.Wishlist
09-10 20:30:15.496: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
09-10 20:30:15.496: E/AndroidRuntime(334):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-10 20:30:15.496: E/AndroidRuntime(334):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-10 20:30:15.496: E/AndroidRuntime(334):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-10 20:30:15.496: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 20:30:15.496: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
09-10 20:30:15.496: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:3683)
09-10 20:30:15.496: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
09-10 20:30:15.496: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:507)
09-10 20:30:15.496: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-10 20:30:15.496: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-10 20:30:15.496: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
09-10 20:30:15.496: E/AndroidRuntime(334): Caused by: java.lang.ClassCastException: com.mypackage.thing.Wishlist
09-10 20:30:15.496: E/AndroidRuntime(334):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
09-10 20:30:15.496: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
09-10 20:30:15.496: E/AndroidRuntime(334):  ... 11 more

应该启动的类或xml文件中有什么问题吗?我正在使用支持包和ActionBarSherlock。我已经尝试检查清单和布局,但似乎没有任何错误。此外,我对片段非常新,所以不确定这些是否正确设置。应该开始的课程有:

   ...
   import com.actionbarsherlock.app.SherlockListFragment;
   import com.mypackage.thing.contentprovider.DatabaseProvider;
   import com.mypackage.thing.database.WishlistTable;

   public class Wishlist extends SherlockListFragment
                implements LoaderManager.LoaderCallbacks<Cursor> {

   ...

   //Inflate ListFragment view
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      return inflater.inflate(R.layout.wishlist, container, false);
   }

   ...

然后将该类设置为使用cursorLoader和provider / cursorAdapter从SQLite填充列表。

1 个答案:

答案 0 :(得分:2)

您的WishList课程延长了SherlockListFragment。这不是Activity课程(请参阅ListFragment

要将其放在Activity中,您必须创建一个托管此片段的新FragmentActivity(或更确切地说,SherlockFragmentActivity)。 This documentation可能会对您有所帮助。