gpt4 book ai didi

android - 在一种情况下出现 RuntimeException。 NullExeption - getBackStackCount() 函数

转载 作者:行者123 更新时间:2023-11-30 01:46:32 25 4
gpt4 key购买 nike

只有当我的应用程序关闭时(为其他应用程序释放资源)并且当我再次打开它时,我才会得到这个提示。如果我自己关闭应用程序,一切正常。

11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: FATAL EXCEPTION: main
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.john.testOne/com.john.testOne.StartPageActivity}: java.lang.NullPointerException
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.os.Looper.loop(Looper.java:130)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:3687)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:507)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: Caused by: java.lang.NullPointerException
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.john.testOne.StartPageActivity.resetToolbarNavigation(StartPageActivity.java:177)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.john.testOne.BaseFragment.onAttach(BaseFragment.java:25)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1030)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1241)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:2048)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentController.dispatchCreate(FragmentController.java:154)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:289)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:61)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.john.testOne.StartPageActivity.onCreate(StartPageActivity.java:34)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.os.Looper.loop(Looper.java:130) 
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:3687) 
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:507) 
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
11-10 22:00:10.007 6255-6255/com.john.testOne E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)

这是我的 Activity :

public class StartPageActivity extends AppCompatActivity implements BaseFragment.BaseFragmentActions {

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_page_activity);;
FragmentManager fm = getSupportFragmentManager();
Fragment mFragment = new MainFragment();
fm.beginTransaction().addToBackStack(null)
.add(R.id.fragment_container,mFragment)
.commit();
initNavigation();
}
}

MainFragment 扩展了 BaseFragment....

和基础 fragment :

public abstract class BaseFragment extends Fragment {
private static String TAG = "BaseFragment";
private BaseFragmentActions baseFragmentActions;
@Override
public void onAttach(Context context) {
super.onAttach(context);
Log.i(TAG, "onAttach = ");
try {
baseFragmentActions = (BaseFragmentActions)(StartPageActivity)getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(((StartPageActivity)getActivity()).toString() + " must implement interface BaseFragmentActions");
}
baseFragmentActions.resetToolbarNavigation(getBackStackCount()!=0);
}

@Override
@CallSuper
public void onDetach() {
super.onDetach();
baseFragmentActions.resetToolbarNavigation((getBackStackCount() -1 )!= 0);

}

private int getBackStackCount() {
int b = getActivity().getSupportFragmentManager().getBackStackEntryCount();

return b;
}

public interface BaseFragmentActions {
public void resetToolbarNavigation(boolean backNavigationEnabled);
}
}

如果需要

baseFragmentActions.resetToolbarNavigation(getBackStackCount()!=0);    

^^
||
||

at com.john.testOne.BaseFragment.onAttach(BaseFragment.java:25)

我添加resetToolbarNavigation.......

        @Override
public void resetToolbarNavigation(boolean backNavigationEnabled) {
mActionBar.setDisplayHomeAsUpEnabled(backNavigationEnabled);//------------------177
// mActionBar.setHomeAsUpIndicator(R.drawable.ic_add);
if (backNavigationEnabled) {
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Log.i("resetToolbarNavigation", "setNavigationOnClickListener");
onBackPressed();
}
});
}
else {
initNavigation();
syncState();
}
}

最佳答案

尝试使用这个:

@Override
public void resetToolbarNavigation(boolean backNavigationEnabled) {
setSupportActionBar(mToolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(backNavigationEnabled);
if (backNavigationEnabled) {
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Log.i("resetToolbarNavigation", "setNavigationOnClickListener");
onBackPressed();
}
});
}
else {
initNavigation();
syncState();
}
}

关于android - 在一种情况下出现 RuntimeException。 NullExeption - getBackStackCount() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33639314/

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