简体   繁体   中英

Android -replace running fragment

I am implementing a navigation drawer into my app, and navigation drawers recomend using fragments, and to just change the running fragment when the user selects something from the navigation drawer. When ever I try to replace the current fragment with a new fragment, my app crashes. here is the code I am using to replace the fragment.

Body newFragment = new Body();

            android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
            android.support.v4.app.FragmentTransaction transaction = fragmentManager
                    .beginTransaction();

            // Replace whatever is in the fragment_container view with this
            // fragment,
            // and add the transaction to the back stack
            transaction.replace(R.id.llhome, newFragment);
            transaction.addToBackStack(null);

            // Commit the transaction
            transaction.commit();

Edit: below is my logcat

06-18 13:53:10.142: E/AndroidRuntime(17700): FATAL EXCEPTION: main
06-18 13:53:10.142: E/AndroidRuntime(17700): java.lang.NullPointerException
06-18 13:53:10.142: E/AndroidRuntime(17700):    at com.OptimusApps.stayhealthy.Body.<init>(Body.java:31)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at com.OptimusApps.stayhealthy.MainActivity$DrawerItemClickListener.onItemClick(MainActivity.java:129)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at android.widget.AbsListView.performItemClick(AbsListView.java:1060)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2516)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at android.widget.AbsListView$1.run(AbsListView.java:3170)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at android.os.Handler.handleCallback(Handler.java:605)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at android.os.Looper.loop(Looper.java:137)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at android.app.ActivityThread.main(ActivityThread.java:4575)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at java.lang.reflect.Method.invokeNative(Native Method)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at java.lang.reflect.Method.invoke(Method.java:511)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-18 13:53:10.142: E/AndroidRuntime(17700):    at dalvik.system.NativeStart.main(Native Method)

I think you have a NPE at this line android.support.v4.app.FragmentTransaction transaction = fragmentManager .beginTransaction();

Replace your android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); by the following one:

android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();

Let me know about your progress ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM