gpt4 book ai didi

android - 启动 Intent 导致 ClassCastException

转载 作者:行者123 更新时间:2023-11-29 21:59:22 25 4
gpt4 key购买 nike

我已经使用 ActionBarSherlock 成功设置了一个 ActionBar,它设置了当我在上面选择一个选项时启动的 Intent 。

@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);
}

问题是当这个 Intent 应该开始时我得到这个错误:

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。我试过检查 list 和布局,但似乎没有任何问题。此外,我对 fragment 非常陌生,所以不确定是否设置正确。应该开始的类(class)有:

   ...
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 填充列表。

最佳答案

您的 WishList 类扩展了 SherlockListFragment。这不是一个 Activity 类(参见 ListFragment

要将其放入 Activity,您必须创建一个新的 FragmentActivity (或者更确切地说,SherlockFragmentActivity)托管此 fragment 。 This documentation可能对你有帮助。

关于android - 启动 Intent 导致 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12357313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com