N
N
Nick20152015-11-11 00:17:32
Android
Nick2015, 2015-11-11 00:17:32

RuntimeException in only one case. NullExeption - getBackStackCount()?

Only then, if my supplement is vinicated (more resources for other programs), and if I renew it, I'll remove all the vines. Even though I myself had closed the appendage, everything was tepid.
The error is only when the add-on is off (because the android was freeing up resources...) and opened it again and this is the action:

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)

This is my active:
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....
this is 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 you need 25 rows (at com.john.testOne.BaseFragment.onAttach(BaseFragment.java:25))
baseFragmentActions.resetToolbarNavigation(getBackStackCount()!=0);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Copperfield, 2015-11-11
@Copperfield

After super.onDetach() getActivity returns null.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question