问题
I have an app that has a bottom bar with three fragments. There are also a few child fragments that are being called from parent fragments. Google Play console shows several crashes due to java.lang.IllegalArgumentException
. I cannot seem to replicate the error, and nothing points to my code in the logs.
Can anybody decipher what could be the problem?
Here is the error stack trace.
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3190)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3300)
at android.app.ActivityThread.access$1000 (ActivityThread.java:211)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1705)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:145)
at android.app.ActivityThread.main (ActivityThread.java:6946)
at java.lang.reflect.Method.invoke (Native Method)
at java.lang.reflect.Method.invoke (Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1199)
Caused by: java.lang.IllegalArgumentException:
at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1293)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState (FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState (FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps (BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps (FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether (FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps (FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions (FragmentManager.java:2013)
at android.support.v4.app.FragmentController.execPendingActions (FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart (FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart (AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart (Instrumentation.java:1264)
at android.app.Activity.performStart (Activity.java:6613)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:3153)
Shown below is the launcher activity code
public void onMenuItemSelected(int itemId) {
Fragment fragment;
switch (itemId) {
case R.id.home:
fragment = new HomeFragment();
if (fragment != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment).commit();
}else {
}
break;
case R.id.profile:
fragment = new ProfileFragment();
if (fragment != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment).commit();
}else {
}
break;
case R.id.account:
fragment = new AccountMainFragment();
if (fragment != null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, fragment).commit();
}else {
//Log.e("MainActivity", "Error in creating fragment");
}
break;
}
When i'm inside a fragment and want to switch to another fragment, this is the code:
SingleProductFragment fragment = new SingleProductFragment();
fragment.setArguments(bundle);
FragmentManager fragmentManager = (((AppCompatActivity) context).getSupportFragmentManager()); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack("single");
fragmentTransaction.replace(R.id.content_frame, fragment);
fragmentTransaction.commit();
When inside a activity and need to call an activity, I do it through the main activity :
Intent intent = new Intent(NotificationListViewActivity.this, AppMainActivity.class);
intent.putExtra("FragmentCheck", "single");
startActivity(intent);
来源:https://stackoverflow.com/questions/45607523/java-lang-illegalargumentexception-when-switching-fragments