我已经使用 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 填充列表。