I'm writing an Android
app using the Navigation Drawer
. I want to change the current fragment
upon a button is clicked.
public void btnClick(){
...
changeFragment();
...
}
private void changeFragment() {
Fragment home = new Home();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.navigation_drawer, home);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
I'm receiving the following error in LogCat
:
FATAL EXCEPTION: main
java.lang.UnsupportedOpetaionException: addView(View) is not supported in AdapterView
My Home
fragment is loaded and displayed at the beginning, and after moving to another fragment and clicking a button I want to automatically return to the Home
fragment.
How can I manage to do it?
EDIT: Full error log
06-28 21:19:26.262: E/AndroidRuntime(5023): FATAL EXCEPTION: main
06-28 21:19:26.262: E/AndroidRuntime(5023): Process: com.susu.appclient, PID: 5023
06-28 21:19:26.262: E/AndroidRuntime(5023): java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.widget.AdapterView.addView(AdapterView.java:461)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:968)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.os.Handler.handleCallback(Handler.java:739)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.os.Handler.dispatchMessage(Handler.java:95)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.os.Looper.loop(Looper.java:135)
06-28 21:19:26.262: E/AndroidRuntime(5023): at android.app.ActivityThread.main(ActivityThread.java:5254)
06-28 21:19:26.262: E/AndroidRuntime(5023): at java.lang.reflect.Method.invoke(Native Method)
06-28 21:19:26.262: E/AndroidRuntime(5023): at java.lang.reflect.Method.invoke(Method.java:372)
06-28 21:19:26.262: E/AndroidRuntime(5023): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-28 21:19:26.262: E/AndroidRuntime(5023): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
If i understand, you are trying to change the fragment below the navigation drawer, and not the navigation drawer itself (which is a lot more complex).
In this line:
ft.replace(R.id.navigation_drawer, home);
You must replace R.id.navigation_drawer for a FragmentLayout that should be on your activitylayout.xml.
Something like:
ft.replace(R.id.fragment_layout, home);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With