Code snippet for changing the application box

I am writing an application Android

using Navigation Drawer

. I want to change the current one fragment

on button click.

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 am getting 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 navigating to another fragment and clicking a button, I want to automatically return to the fragment Home

.

How can i do this?

EDIT: Complete 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)

      

+3


source to share


1 answer


If I understand you are trying to change the fragment below the navigation box, not the navigation box itself (which is much more complicated).

In this line:

 ft.replace(R.id.navigation_drawer, home);

      



You have to replace R.id.navigation_drawer for FragmentLayout, which should be in your activitylayout.xml file.

Something like:

ft.replace(R.id.fragment_layout, home);

      

+1


source







All Articles