我需要打开另一个片段。 我用这个代码
Fragment newFragment = new TabFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.viewpager, newFragment);
transaction.addToBackStack(null);
transaction.commit();
但它不起作用。这是错误日志:
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)
进入“transaction.replace(R.id.viewpager,newFragment);”我应该指定什么?还有其他问题吗?
答案 0 :(得分:1)
试试这个:
private FragmentManager mFragmentManager = this.getFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView, new NewFragment()).commit();
答案 1 :(得分:0)
您似乎没有在ID" viewpager"的片段中查看视图。 在片段内添加一个ViewGroup(例如一个FrameLayout)作为要放置的新片段的容器。