Reputation: 2621
Currently working on an app, which has a MainActivity with a Fragment that contains multiple buttons. I cannot start another activity when clicking on the buttons.
I keep getting an error here saying
java.lang.IllegalArgumentException: No view found for id 0x7f090057 for fragment
I have two classes; a NavigationDrawer class (my MainActivity)
And a class named StartingFragment (which I want to be the main view when the drawer is closed/not active).
Here is the code that the error seems to refer to (from NavDrawer.java):
/** Swaps fragments in the main content view */
/** Starts an Activity when item is clicked */
private void selectItem(int position) {
// Create a new fragment and specify the tea type
// to show based on position
Fragment fragment = new StartingFragment();
Bundle args = new Bundle();
args.putInt(StartingFragment.TEA_TYPE_POS, position);
fragment.setArguments(args);
// Insert the fragment by replacing any existing fragment
FragmentManager fragManager = getFragmentManager();
fragManager.beginTransaction().replace(R.id.fragment_replace, fragment)
.commit();
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(navDrawerTitles[position]);
navDrawerLayout.closeDrawer(mDrawerList);
}
I looked at this question here: https://stackoverflow.com/a/8158916 and I see that R.id.fragment_replace should be a child of the R.layout.nav_drawer, which is the case here. I also posted a very similar question a few days ago, although previously the app would just crash from the start; now it crashes on Button/AlertDialog clicks
However, I don't know what to adjust in my code. I get this error whenever I try to click on a button (and then click on the neutral/positive button of the AlertDialog that follows) from StartingFragment.java & starting_fragment.xml
Logcat Text:
01-02 17:16:13.642: E/AndroidRuntime(26814): FATAL EXCEPTION: main 01-02 17:16:13.642: E/AndroidRuntime(26814): Process: com.fv4.tea.teatime.first, PID: 26814 01-02 17:16:13.642: E/AndroidRuntime(26814): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fv4.tea.teatime.first/com.fv4.teatime.tea.app.OolongTeaActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f090057 (com.fv4.tea.teatime.first:id/fragment_replace) for fragment StartingFragment{41c95b40 #0 id=0x7f090057} 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212) 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.app.ActivityThread.access$800(ActivityThread.java:144) 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.os.Handler.dispatchMessage(Handler.java:102) 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.os.Looper.loop(Looper.java:136) 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.app.ActivityThread.main(ActivityThread.java:5146) 01-02 17:16:13.642: E/AndroidRuntime(26814): at java.lang.reflect.Method.invokeNative(Native Method) 01-02 17:16:13.642: E/AndroidRuntime(26814): at java.lang.reflect.Method.invoke(Method.java:515) 01-02 17:16:13.642: E/AndroidRuntime(26814): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) 01-02 17:16:13.642: E/AndroidRuntime(26814): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) 01-02 17:16:13.642: E/AndroidRuntime(26814): at dalvik.system.NativeStart.main(Native Method) 01-02 17:16:13.642: E/AndroidRuntime(26814): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f090057 (com.fv4.tea.teatime.first:id/fragment_replace) for fragment StartingFragment{41c95b40 #0 id=0x7f090057} 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:882) 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062) 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.app.BackStackRecord.run(BackStackRecord.java:684) 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447) 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.app.Activity.performStart(Activity.java:5240) 01-02 17:16:13.642: E/AndroidRuntime(26814): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178) 01-02 17:16:13.642: E/AndroidRuntime(26814): ... 11 more
Upvotes: 0
Views: 6435
Reputation: 2621
I just fixed the issue! I was writing extends NavigationDrawer
on all of my classes, and so when I changed it to extends Activity
, that fixed the problem, and now everything is working smoothly. All set now.
Upvotes: 1
Reputation: 3885
You cannot replace a fragment that is statically placed in an xml layout file. You should create a container (e.g. a ViewGroup) in the layout and then add the fragment programatically using FragmentTransaction.
Upvotes: 0