gpt4 book ai didi

java - Android - 替换运行 fragment

转载 作者:行者123 更新时间:2023-12-01 14:25:59 25 4
gpt4 key购买 nike

我正在我的应用程序中实现一个抽屉导航,抽屉导航建议使用 fragment ,并在用户从抽屉导航中选择某些内容时更改正在运行的 fragment 。当我尝试用新 fragment 替换当前 fragment 时,我的应用程序就会崩溃。这是我用来替换 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();

编辑:下面是我的 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)

最佳答案

我认为你在这一行有一个NPE android.support.v4.app.FragmentTransaction transaction =fragmentManager .beginTransaction();

android.support.v4.app.FragmentManagerfragmentManager = getSupportFragmentManager(); 替换为以下内容:

android.support.v4.app.FragmentManagerfragmentManager = getFragmentManager();

让我知道你的进展;)

关于java - Android - 替换运行 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17175616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com