gpt4 book ai didi

java - 在创建 fragment 之前调用 fragment 内的方法,抛出 NPE。我应该如何处理这个问题?

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

我试图基本上“根据命令”隐藏/显示操作栏按钮。我必须从应用程序内的不同 fragment 执行此操作,因此我创建了一个方法来在按钮所在的 fragment 中切换按钮。

如下所示,一旦从抽屉导航中选择 fragment ,我就会调用 fragment 内的方法 addSMVFFragments,然后调用 disableDeleteAllButton 方法。因此,当调用 disableDeleteAllButton 方法时, fragment 尚未创建,这会引发 NPE

我该如何解决这个问题?我可以“延迟”对 disableDeleteAllButton 的调用,以便有时间创建 fragment (对我来说似乎是个坏主意)?我应该做点别的吗?非常感谢所有帮助。

(所有代码都经过简化以便于阅读,因此如果某些部分似乎缺失,它们可能存在于完整的代码中。如果您希望查看整套代码,请告诉我。)

主要 Activity :

@Override
public boolean onNavigationItemSelected(MenuItem item) {

FragmentManager fm = getFragmentManager();
isMainShown = false;

int id = item.getItemId();

if (id == R.id.nav_stuffmanager) {
fm.beginTransaction().replace(R.id.content_frame, new StuffManagerFragment(), getString(R.string.navdrawer_stuffmanager)).commit();
setTitle(getString(R.string.navdrawer_stuffmanager));
addSMVFFragments();
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

public void addSMVFFragments(){
StuffManagerFragment.disableDeleteAllButton(true);
}

StuffManagerFragment:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.stuffmanager_actionbuttons, menu);
optionsMenu = menu;
}

....

public static void disableDeleteAllButton(boolean bool) {
optionsMenu.findItem(R.id.removeAllButton).setVisible(!bool);
}

日志:

03-01 02:07:49.390 15748-15748/com.example.tim.timapp I/art: Not late-enabling -Xcheck:jni (already on)
03-01 02:07:49.460 15748-15748/com.example.tim.timapp W/System: ClassLoader referenced unknown path: /data/app/com.example.tim.timapp-2/lib/x86_64
03-01 02:07:49.650 15748-15777/com.example.tim.timapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-01 02:07:49.690 15748-15777/com.example.tim.timapp I/OpenGLRenderer: Initialized EGL, version 1.4
03-01 02:07:49.750 15748-15777/com.example.tim.timapp W/EGL_emulation: eglSurfaceAttrib not implemented
03-01 02:07:49.750 15748-15777/com.example.tim.timapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f63f316cd80, error=EGL_SUCCESS
03-01 02:08:06.360 15748-15748/com.example.tim.timapp D/TEST: Fragment not ready
03-01 02:08:16.270 15748-15748/com.example.tim.timapp D/AndroidRuntime: Shutting down VM
03-01 02:08:16.270 15748-15748/com.example.tim.timapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tim.timapp, PID: 15748
java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setVisible(boolean)' on a null object reference
at com.example.fragments.Settings.StuffManagerFragment.disableDeleteAllButton(StuffManagerFragment.java:98)
at com.example.tim.timapp.MainActivity.addSMVFFragments(MainActivity.java:209)
at com.example.tim.timapp.MainActivity.onNavigationItemSelected(MainActivity.java:191)
at android.support.design.widget.NavigationView$1.onMenuItemSelected(NavigationView.java:150)
at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
at android.support.v7.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:84)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:308)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

fragment 是异步添加的,因此在将其添加到堆栈(fragmentManager.replace)后无法立即调用 API,因为它尚未初始化(尚未经历膨胀)。您应该通过工厂方法将变量传递给 fragment ,该变量将指示 fragment 显示或隐藏菜单选项。

关于java - 在创建 fragment 之前调用 fragment 内的方法,抛出 NPE。我应该如何处理这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35712915/

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