DMCApps / NavigationFragment

The purpose of this manager is to work with the ViewPager, Tabs and Navigation drawer to handle a single stack flow of fragments on the screen. It makes use of a main Fragment as a container and presents and hides fragments within it as children.
MIT License
28 stars 8 forks source link

attempted to write mNextAnim with nullpointerexception #11

Closed jjhesk closed 7 years ago

jjhesk commented 8 years ago

There was a null pointer exception pops out from the system while i compile this library at the start. baconlmy48yhesk02262016170032

which i cannot tell where it was coming from basically it was the last lines of code before it happens.


    /**
     * require android-support-v4 import and the regular android fragment
     *
     * @param fragment    the unknown typed fragment
     * @param title       the string in title
     * @param oldFragment the previous fragment
     * @param closeDrawer if it needs to close the drawer after the new fragment has been rendered
     */
    public void setFragment(F fragment, String title, @Nullable F oldFragment, boolean closeDrawer) {
        currentFragmentNow = fragment;
        setTitle(title);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            // before honeycomb there is not android.app.Fragment
            android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if (oldFragment != null && oldFragment != fragment)
                ft.remove((android.support.v4.app.Fragment) oldFragment);

            ft.replace(targetHomeFrame(), (android.support.v4.app.Fragment) fragment).commit();
        } else if (fragment instanceof android.app.Fragment) {
            if (oldFragment instanceof android.support.v4.app.Fragment)
                throw new RuntimeException("You should use only one type of Fragment");

            android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (oldFragment != null && fragment != oldFragment)
                ft.remove((android.app.Fragment) oldFragment);

            ft.replace(targetHomeFrame(), (android.app.Fragment) fragment).commit();
        } else if (fragment instanceof android.support.v4.app.Fragment) {
            if (oldFragment instanceof android.app.Fragment)
                throw new RuntimeException("You should use only one type of Fragment");

            android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if (oldFragment != null && oldFragment != fragment)
                ft.remove((android.support.v4.app.Fragment) oldFragment);
            ft.replace(targetHomeFrame(), (android.support.v4.app.Fragment) fragment).commit();
        } else
            throw new RuntimeException("Fragment must be android.app.Fragment or android.support.v4.app.Fragment");

        //if (closeDrawer)
        // getSlidingMenu().toggle(true);

        notifyOnBodyFragmentChange(currentFragmentNow);
    }

the issue triggers at ft.replace(targetHomeFrame(), (android.support.v4.app.Fragment) fragment).commit(); but it was not constantly happening.

jjhesk commented 8 years ago

  public void setFragment(F fragment, String title, @Nullable F oldFragment, boolean closeDrawer) {
        currentFragmentNow = fragment;
        setTitle(title);
        if (fragment instanceof NavigationFragment) {
            NavigationFragment nav =(NavigationFragment)fragment;
            fmstack = SingleStackNavigationManagerFragment.newInstance(nav);
            fmstack.setDefaultPresentAnimations(R.anim.popup_show, R.anim.popup_hide);
            fmstack.setDefaultDismissAnimations(R.anim.bottom_down, R.anim.bottom_up);
            //generate ID for the navigation fragment
            mSingleStackNavigationManagerFragmentTag = UUID.randomUUID().toString();
            android.support.v4.app.FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
            if (currentFragmentNow == null) {

            }
            currentFragmentNow = fragment;
            fm.replace(targetHomeFrame(), fmstack, mSingleStackNavigationManagerFragmentTag);
            fm.commit();

        }else if (fragment instanceof android.app.Fragment) {
            if (oldFragment instanceof android.support.v4.app.Fragment)
                throw new RuntimeException("You should use only one type of Fragment");

            android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (oldFragment != null && fragment != oldFragment)
                ft.remove((android.app.Fragment) oldFragment);

            ft.replace(targetHomeFrame(), (android.app.Fragment) fragment).commit();
        } else if (fragment instanceof android.support.v4.app.Fragment) {
            if (oldFragment instanceof android.app.Fragment)
                throw new RuntimeException("You should use only one type of Fragment");

            android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if (oldFragment != null && oldFragment != fragment)
                ft.remove((android.support.v4.app.Fragment) oldFragment);
            ft.replace(targetHomeFrame(), (android.support.v4.app.Fragment) fragment).commit();
        } else 
            throw new RuntimeException("Fragment must be android.app.Fragment or android.support.v4.app.Fragment");

        //if (closeDrawer)
        // getSlidingMenu().toggle(true);

        notifyOnBodyFragmentChange(currentFragmentNow);
    }

This is the final piece that is written. Is it okay to switch between from non-navigation fragment and navigation fragment? #3 ?

jjhesk commented 8 years ago

i think #3 is not the case because it has been resolved. However these is another bug occurring after this line or sometimes earlier. I am trying to think of any issues from my side that has to deal with nextAnim.. I am not sure where is it coming from.The only thing that will use the frag manager would the smarttablayout.. i afraid this is the case that smarttablayout is crashing heavily with this library.

jjhesk commented 8 years ago

fixed the issues related the close and open

DMCApps commented 8 years ago

Can you please elaborate on what the issue was? If it wasn't with the library then this will help others fix the same issue in their project. If it is with the issue I'd like to incorporate any work that you are doing on the library. That way everyone can get the benefits of fixes that you have!

I have re-opened the issue until I know for certain it is not the library code or that it is changed in the library.

Please note all development is taking place on the develop branch. I'd like to keep the master clean as the current live code so that I can manage any hot fixes from there (such as this fix you are talking about and #10)

jjhesk commented 8 years ago

yes. I will post the code out later. I need to first re-organize my coding block.

jjhesk commented 8 years ago

basically if there are coding that has been written in the onresume and onpause to dealing with the normal fragments that has been initialized in the framelayout and the crash will incurr as mentioned above. I resolved this crash by removed all those lines that has to do with the normal fragments.

DMCApps commented 8 years ago

I'm not quite sure the use cases that you are having trouble with normal fragments. Any Fragment that is presented from the NavigationManagerFragment must be a NavigationFragment or a NavigationListFragment. This is what allows the present/dismiss methods to call to the parent NavigationManagerFragment to remove/add and attach/detach fragments depending on what part of the process you are performing.

Normal fragment can be used to replace/add to the same FrameLayout as the NavigationManagerFragment, but NavigationFragments have no additional functionality without a NavigationManagerFragment as their parent. All this happens in the background when you call present.

When the NavigationManagerFragment adds the given fragment to the stack, it is the parent of the fragment and also assigns itself using the setNavigationManager call when the new NavigationFragment is presented. This allows the present/dismiss methods to call back to the manager when we need to push/pop a fragment from the stack.

jjhesk commented 8 years ago

@DMan654 @DMCApps hi there is another issue reappeared again with this.. java.lang.NullPointerException: Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim' on a null object reference

07-07 12:32:48.027 13206-13206/xxxxxxxx E/AndroidRuntime: FATAL EXCEPTION: main Process:xxxxxxxx , PID: 13206

java.lang.NullPointerException: Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim' on a null object reference at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1671) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:532) 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:5422) 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)

I have done some testing on android support v4-24.0.0 BackStackRecord.java line 488.


    public FragmentTransaction detach(Fragment fragment) {
        Op op = new Op();
        op.cmd = OP_DETACH;
        op.fragment = fragment;
        addOp(op);

        return this;
    }

This call has triggered 3 times when start an intent activity from another navigation fragment. The architecture based on starting an AppCompatActivity A with SingleStackNavigationManagerFragment and start another AppCompatActivity B with SingleStackNavigationManagerFragment from NavigationFragment. On the activity B we start another fragmentActivity from the one of the NavigationFragment without SingleStackNavigationManagerFragment.

3 Attempts from calling detach

  1. not null - navigation fragment from activity b
  2. input null
  3. not null

version: 0.2.0.3, 0.3.0

jjhesk commented 8 years ago

The above issue was resolved by taking another fragment lunching in the navigation fragment. Maybe there should be able to get some suggestion from the exception catch.

DMCApps commented 7 years ago

I'm going to mark this as complete. It looks like there haven't been many occurrences of this reported lately. Please reopen if there is any other instances coming up.