[英]Android unfortunately has stopped when I want to push fragment
If I want to change fragment when user has selected some elements of menu (android navigation drawer) my app crashing. 如果我想在用户选择菜单的某些元素(Android导航抽屉)时更改片段,我的应用程序将崩溃。
Here is my fragment class code 这是我的片段类代码
@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);
}
}
It's very simple code. 这是非常简单的代码。
Here what i do on my main class with drawer: 这是我在带抽屉的主班上所做的事情:
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);
}
}
It is very simple code too. 这也是非常简单的代码。 And here is my errors:
这是我的错误:
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
What am I doing wrong? 我究竟做错了什么?
As stated in your logcat output 如您的logcat输出中所述
java.lang.ClassCastException: com.example.android.navigationdrawerexample.InboxActivity cannot be cast to android.app.Activity
java.lang.ClassCastException:com.example.android.navigationdrawerexample.InboxActivity无法转换为android.app.Activity
You're trying to start your fragment as an activity 您正在尝试将片段作为一项活动开始
Intent activity = new Intent(getApplicationContext(), InboxActivity.class);
startActivity(activity);
You should remove those two lines as your fragment has already been added using a FragmentTransaction
. 您应该删除这两行,因为已经使用
FragmentTransaction
添加了您的FragmentTransaction
。
You're doing a bit of overkill here. 您在这里做的有些过分了。 First, you create the fragment and replace it through a transaction, which is all correct.
首先,创建片段并通过事务将其替换,这都是正确的。 However after that, you're trying to start your fragment class as an activity.
但是,此后,您尝试将片段类作为活动开始。 A fragment is not an activity, so that's why you get this in your error log:
片段不是活动,所以这就是为什么要在错误日志中得到它:
Caused by: java.lang.ClassCastException: com.example.android.navigationdrawerexample.InboxActivity cannot be cast to android.app.Activity
The solution is fairly simple, just remove these two lines: 解决方案非常简单,只需删除以下两行:
Intent activity = new Intent(getApplicationContext(), InboxActivity.class);
startActivity(activity);
Protip: rename InboxActivity
to InboxFragment
. 提示:将
InboxActivity
重命名为InboxFragment
。 Confusing names are the worst. 令人困惑的名字是最糟糕的。 ;-)
;-)
public class InboxActivity extends Fragment
您的InboxActivity
应该扩展Activity以便以您尝试的方式启动它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.