I am new to Android and due help from fellow SOians, I am able to proceed well. I am implementing AppBar items for navigating to respective fragments from MainActivity. The following code is part of MainActivity.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up buttonAction, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
Fragment fragment = new SettingsFragment();
FragmentManager fragmentManager = this.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.SettingsFragment, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
break;
case R.id.action_kyc:
// go to kycfragment
break;
case R.id.action_about:
// go to aboutfragment
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
I am getting Illegal Argument Exception "No view found for id 0x7f0a000e for fragment SettingsFragment"
Browsed a lot here in SO as well as outside. None of them helped me.
Please let me know what is causing the error and how to fix it.
The stack extract is below:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ptimer, PID: 7759
java.lang.IllegalArgumentException: No view found for id 0x7f0a000e (com.example.ptimer:id/SettingsFragment) for fragment SettingsFragment{5c8b099} (86b09f6f-49b4-470e-9753-e5acd603164c id=0x7f0a000e)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:513)
at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
I/Process: Sending signal. PID: 7759 SIG: 9
CodePudding user response:
How does your MainActivity
XML layout look like?
Citing your code:
fragmentTransaction.replace(R.id.SettingsFragment, fragment);
Your problem is most likely that there is no view with id SettingsFragment
in your layout.
The first argument given to replace
should point to the fragment container in your layout file. E.g. if your layout includes:
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.ExampleFragment" />
Then it would be fragmentTransaction.replace(R.id.fragment_container, fragment);
See also https://developer.android.com/guide/fragments/create#add and https://developer.android.com/guide/fragments/transactions#add-remove
Depending on your use case, you could also open your settings in a new activity instead of replacing a fragment inside the MainActivity
.