-1

I have Navigation Drawer menu in that I have fragments Frag1,Frag2 and Frag3. Frag2 is being replaced with Frag21, Frag21 with Frag22 and so on.
If I press back button from Frag22 to Frag21 and Frag2 the app closes properly.
But if I go directly from Frag22 to Frag1 and try to close the app, its getting crashed and I m getting the error

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalArgumentException: No view found for id 0x7f0f00c4 (com.NuSS.MyPAS:id/root_frame) for fragment CategoriesDisplayList{4260ca00 #1 id=0x7f0f00c4}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:960)
at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1553)
at android.support.v4.app.FragmentManagerImpl$2.run(FragmentManager.java:497)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5306)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)**

Have also given

@Override
public void onBackPressed() {
    if(fragmentManager.getBackStackEntryCount() != 0) {
        fragmentManager.popBackStack();
    } else {
        super.onBackPressed();
    }
}

in MainFragment Activity.

Kindly give solution stuck up with this error for few days.

UPDATE

Code snippets

RootFragment

public class RootFragment extends Fragment  {

    private static final String TAG = "RootFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        /* Inflate the layout for this fragment */
        View view = inflater.inflate(R.layout.activity_root_fragment, container, false);

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        /*
         * When this container fragment is created, we fill it with our first
         * "real" fragment
         */
        transaction.replace(R.id.root_frame, new CategoriesDisplayList());

        transaction.commit();

        return view;
    }

    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        super.onDestroyView();

        try {
            Fragment fragment = (getFragmentManager()
                    .findFragmentById(R.id.root_frame));
            FragmentTransaction ft = getActivity().getSupportFragmentManager()
                    .beginTransaction();
            ft.remove(fragment);
            ft.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

CategoryDisplayList

public class CategoriesDisplayList extends Fragment implements SwipeRefreshLayout.OnRefreshListener{

    categoryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentTransaction trans = getFragmentManager().beginTransaction();

            trans.replace(R.id.root_frame, new RootFragmentService());

            trans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            trans.addToBackStack(null);
            trans.commit();
        }
    });

    //registerForContextMenu(vendorListView);

    return rootView;
}

RootFragmentService

public class RootFragmentService extends Fragment {

    private static final String TAG = "RootFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_root_fragment, container, false);

        FragmentTransaction transaction = getFragmentManager()
                .beginTransaction();
        /*
         * When this container fragment is created, we fill it with our first
         * "real" fragment
         */
        transaction.replace(R.id.root_frame, new ServicesList());

        transaction.commit();

        return view;
    }

    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        super.onDestroyView();

        try {
            Fragment fragment = (getFragmentManager()
                    .findFragmentById(R.id.root_frame));
            FragmentTransaction ft = getFragmentManager()
                    .beginTransaction();
            ft.remove(fragment);
            ft.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Thanks in advance.

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
samz22
  • 101
  • 2
  • 12
  • http://stackoverflow.com/questions/7508044/android-fragment-no-view-found-for-id – IntelliJ Amiya Jan 08 '16 at 11:10
  • Hi, how are you closing your app? Just by clicking back button repeatedly? – nstosic Jan 08 '16 at 11:11
  • http://stackoverflow.com/questions/28637046/illegalargumentexception-no-view-found-for-id-in-fragments – Enrique Quero Jan 08 '16 at 11:14
  • From your log I assume that there is an instance of `CategoriesDisplayList` that is bound to a view inside `Frag2` and it's required in your `onPause()`, `onStop()` or `onDestroy()` logic but the VM is not able to retrieve it since `Frag2` instance isn't attached to the view hierarchy in the second case you described. If that's the case, please post the relevant code snippet(s) . – nstosic Jan 08 '16 at 11:16
  • Yes by clicking the back button repeatedly – samz22 Jan 08 '16 at 11:16
  • are you override onDestroy() in MainFragment activity? – Radesh Sep 08 '18 at 06:33

2 Answers2

0

make an onClickButton listener to the back button more info in android developers: View.OnClickListener

ToniApps
  • 88
  • 1
  • 11
0

if you want to go to the back-stack you don't need call popBackStack on onBackPressed event. if you use addToBackStack method on your transAction, onBackPressed will do it without extra code.

fragmentManager.beginTransaction().setCustomAnimations(animationIn, animationOut).replace(layout, fragment, tag).addToBackStack(backStackTag).commit();
SinaMN75
  • 6,742
  • 5
  • 28
  • 56