问题
Only when my app was off (freed resourses for other apps) and when i again opened it, i got this exaption. If i myself closed app, all it worked.
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)
It's my 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 extends BaseFragment....
and BaseFragment:
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);
}
}
If will need
baseFragmentActions.resetToolbarNavigation(getBackStackCount()!=0);
^^
||
||
at com.john.testOne.BaseFragment.onAttach(BaseFragment.java:25)
I add 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();
}
}
回答1:
Try to use this:
@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();
}
}
来源:https://stackoverflow.com/questions/33639314/runtimeexception-in-one-case-nullexeption-getbackstackcount