If I want to change fragment when user has selected some elements of menu (android navigation drawer) my app crashing.
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
java.lang.ClassCastException: com.example.android.navigationdrawerexample.InboxActivity cannot be cast to 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
.
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
. Confusing names are the worst. ;-)
public class InboxActivity extends Fragment
您的InboxActivity
应该扩展Activity以便以您尝试的方式启动它
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.