Тільки тоді, коли моє додаток був вимкнений (звільнений ресурси для інших програм), і коли я знову відкрив його, я отримав це виняток. Якщо я сам закрив додаток, все це працювало.
Ошибка только тогда когда додаток Офнулся(поскольку андроид освобождал ресурсы...) и снова открыл его и вот это т екшепшин:
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)
Это моя активносить:
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....
это 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);
}
}
Если надо 25 рядок (at com.john.testOne.BaseFragment.onAttach(BaseFragment.java:25))
baseFragmentActions.resetToolbarNavigation(getBackStackCount()!=0);