gpt4 book ai didi

java - 不幸的是,当我想推送 fragment 时,Android 停止了

转载 作者:行者123 更新时间:2023-12-01 13:06:41 25 4
gpt4 key购买 nike

如果我想在用户选择菜单(android 抽屉导航)的某些元素时更改 fragment ,我的应用程序崩溃。

这是我的 fragment 类代码

@SuppressLint("NewApi")
public class InboxActivity extends Fragment
{
@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.inbox_fragment, container, false);

}

}

这是非常简单的代码。

这是我在带抽屉的主课上所做的事情:

private void selectItem(int position) {


Fragment f;
if(position == 1)
{
f = new InboxActivity();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.content_frame, f);
ft.commit();

Intent activity = new Intent(getApplicationContext(), InboxActivity.class);
startActivity(activity);
}
}

这也是非常简单的代码。这是我的错误:

04-22 11:18:25.830: E/AndroidRuntime(25512): FATAL EXCEPTION: main
04-22 11:18:25.830: E/AndroidRuntime(25512): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.android.navigationdrawerexample/com.example.android.navigationdrawerexample.InboxActivity}: java.lang.ClassCastException: com.example.android.navigationdrawerexample.InboxActivity cannot be cast to android.app.Activity
04-22 11:18:25.830: E/AndroidRuntime(25512): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-22 11:18:25.830: E/AndroidRuntime(25512): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
04-22 11:18:25.830: E/AndroidRuntime(25512): at android.app.ActivityThread.access$700(ActivityThread.java:154)
04-22 11:18:25.830: E/AndroidRuntime(25512): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
04-22 11:18:25.830: E/AndroidRuntime(25512): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 11:18:25.830: E/AndroidRuntime(25512): at android.os.Looper.loop(Looper.java:137)
04-22 11:18:25.830: E/AndroidRuntime(25512): at android.app.ActivityThread.main(ActivityThread.java:5306)
04-22 11:18:25.830: E/AndroidRuntime(25512): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 11:18:25.830: E/AndroidRuntime(25512): at java.lang.reflect.Method.invoke(Method.java:511)
04-22 11:18:25.830: E/AndroidRuntime(25512): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-22 11:18:25.830: E/AndroidRuntime(25512): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-22 11:18:25.830: E/AndroidRuntime(25512): at dalvik.system.NativeStart.main(Native Method)
04-22 11:18:25.830: E/AndroidRuntime(25512): Caused by: java.lang.ClassCastException: com.example.android.navigationdrawerexample.InboxActivity cannot be cast to android.app.Activity
04-22 11:18:25.830: E/AndroidRuntime(25512): at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
04-22 11:18:25.830: E/AndroidRuntime(25512): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
04-22 11:18:25.830: E/AndroidRuntime(25512): ... 11 more

我做错了什么?

最佳答案

如 logcat 输出中所述

java.lang.ClassCastException: com.example.android.navigationdrawerexample.InboxActivity cannot be cast to android.app.Activity

您正在尝试将 fragment 作为 Activity 启动

Intent activity = new Intent(getApplicationContext(), InboxActivity.class);
startActivity(activity);

您应该删除这两行,因为您的 fragment 已使用 FragmentTransaction 添加。

关于java - 不幸的是,当我想推送 fragment 时,Android 停止了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23215400/

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