找不到片段的 ID 0x7f0800c4 的视图
No view found for id 0x7f0800c4 for fragment
当我尝试在滑动选项卡 viewPager
中从子片段 (ClassroomsTab
) 替换父片段 (SearchFragment
) 时出现此错误。基本上我想要实现的是在每个子片段中有一个滑动选项卡菜单和多个按钮,关闭滑动选项卡菜单并打开一个不同的现有片段。 SearchFragment
在导航抽屉中 activity 按钮的目标片段也是如此。
据我所知,解决此问题的方法是将这行代码中的 getChildFragmentManager()
替换为 getActivity().getSupportFragmentManager()
或 getFragmentManager()
:viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
。这解决了应用程序崩溃的问题,但是当我通过新片段的导航抽屉单击搜索选项卡时,大多数带有按钮的滑动选项卡子片段都不会显示。
感谢您的帮助:)
SearchFragment
OnCreateView
代码:
public TabLayout tabLayout;
public ViewPager viewPager;
public static int int_items = 5;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//this inflates the tab layout file.
View view = inflater.inflate(R.layout.search_layout,null);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
// this puts the titles on the top
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
//this preloads the other tabs. currently only have it set to one for testing
viewPager.setOffscreenPageLimit(1);
return view;
}
ClassroomsTab
代码(我测试时现在只有一个按钮):
注意:content_frame
是导航的框架布局activity
public class ClassroomsTab extends Fragment {
View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.classrooms, container, false);
defineButtons();
return myView;
}
public void defineButtons() {
Button CR1C1 = (Button)myView.findViewById(R.id.CR1C1);
CR1C1.setOnClickListener(buttonClickListener);
}
private View.OnClickListener buttonClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentmanager = getFragmentManager();
switch (view.getId()) {
case R.id.CR1C1:
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
,new MapFragment())
.commit();
break;
}
}
};
完全错误
09-08 15:52:13.154 12220-12220/com.example.jeremy.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jeremy.app, PID: 12220
java.lang.IllegalArgumentException: No view found for id 0x7f0800c4 (com.example.jeremy.app:id/content_frame) for fragment MapFragment{c7d968f #2 id=0x7f0800c4}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1417)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1754)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1822)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2591)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2378)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2333)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2240)
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:703)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这通常发生在您更改多个事务中的布局时。
同样的事情发生在我身上,我通过清除它后面的堆栈来解决它。但您必须对所有交易使用一个 FragmentManager
。
完成交易后输入此代码
private void clearStack() {
if (fragmentManager.getBackStackEntryCount() > 0) {
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
fragmentManager.popBackStack();
}
}
if (fragmentManager.getFragments().size() > 0) {
for (int i = 0; i < fragmentManager.getFragments().size(); i++) {
Fragment mFragment = fragmentManager.getFragments().get(i);
if (mFragment != null) {
fragmentManager.beginTransaction().remove(mFragment).commit();
}
}
}
}
当我尝试在滑动选项卡 viewPager
中从子片段 (ClassroomsTab
) 替换父片段 (SearchFragment
) 时出现此错误。基本上我想要实现的是在每个子片段中有一个滑动选项卡菜单和多个按钮,关闭滑动选项卡菜单并打开一个不同的现有片段。 SearchFragment
在导航抽屉中 activity 按钮的目标片段也是如此。
据我所知,解决此问题的方法是将这行代码中的 getChildFragmentManager()
替换为 getActivity().getSupportFragmentManager()
或 getFragmentManager()
:viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
。这解决了应用程序崩溃的问题,但是当我通过新片段的导航抽屉单击搜索选项卡时,大多数带有按钮的滑动选项卡子片段都不会显示。
感谢您的帮助:)
SearchFragment
OnCreateView
代码:
public TabLayout tabLayout;
public ViewPager viewPager;
public static int int_items = 5;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//this inflates the tab layout file.
View view = inflater.inflate(R.layout.search_layout,null);
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
// this puts the titles on the top
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
//this preloads the other tabs. currently only have it set to one for testing
viewPager.setOffscreenPageLimit(1);
return view;
}
ClassroomsTab
代码(我测试时现在只有一个按钮):
注意:content_frame
是导航的框架布局activity
public class ClassroomsTab extends Fragment {
View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.classrooms, container, false);
defineButtons();
return myView;
}
public void defineButtons() {
Button CR1C1 = (Button)myView.findViewById(R.id.CR1C1);
CR1C1.setOnClickListener(buttonClickListener);
}
private View.OnClickListener buttonClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentmanager = getFragmentManager();
switch (view.getId()) {
case R.id.CR1C1:
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
,new MapFragment())
.commit();
break;
}
}
};
完全错误
09-08 15:52:13.154 12220-12220/com.example.jeremy.app E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.jeremy.app, PID: 12220 java.lang.IllegalArgumentException: No view found for id 0x7f0800c4 (com.example.jeremy.app:id/content_frame) for fragment MapFragment{c7d968f #2 id=0x7f0800c4} at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1417) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1754) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1822) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2591) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2378) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2333) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2240) at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:703) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
这通常发生在您更改多个事务中的布局时。
同样的事情发生在我身上,我通过清除它后面的堆栈来解决它。但您必须对所有交易使用一个 FragmentManager
。
完成交易后输入此代码
private void clearStack() {
if (fragmentManager.getBackStackEntryCount() > 0) {
for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
fragmentManager.popBackStack();
}
}
if (fragmentManager.getFragments().size() > 0) {
for (int i = 0; i < fragmentManager.getFragments().size(); i++) {
Fragment mFragment = fragmentManager.getFragments().get(i);
if (mFragment != null) {
fragmentManager.beginTransaction().remove(mFragment).commit();
}
}
}
}