Simone Simone - 1 month ago 24
Android Question

Open Fragment from fragment error

I need to open a fragment from another one.
I use this code

Fragment newFragment = new TabFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.viewpager, newFragment);
transaction.addToBackStack(null);
transaction.commit();


But it doesn't work. This is error log:

FATAL EXCEPTION: main
Process: simone.biliato.fortitudobasketvedelago, PID: 4166
java.lang.IllegalArgumentException: No view found for id 0x7f0d00ab (simone.biliato.fortitudobasketvedelago:id/viewpager) for fragment TabFragment{12144aca #1 id=0x7f0d00ab}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)


Into "transaction.replace(R.id.viewpager, newFragment);" what should I specify? Anyother has the same problem?

Answer

Try this:

private FragmentManager mFragmentManager = this.getFragmentManager();

FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new NewFragment()).commit();