Eug*_*vin 0 android android-fragments android-actionbar android-tabs
这个问题已在这里被多次提出,但我找不到任何帮助.我正在编写一个Android应用程序,似乎在ActionBar上创建选项卡时遇到问题.我一直在寻找问题几个小时似乎无法找到解决方案,但我认为我确实找到了源.我甚至已经清空了实现只是为了获得一些结果并在它们的基础上构建,但是我没有发现任何东西.
这是创建选项卡片段并插入操作栏
// put ActionBar to nav. mode
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// init "YES" tab
Fragment yesTabFragment = Fragment.instantiate(this, YesTabFragment.class.getName());
FlowTabListener yesTabListener = new FlowTabListener(yesTabFragment);
ActionBar.Tab yesTab = actionBar.newTab().setText("YES").setTabListener(yesTabListener);
actionBar.addTab(yesTab);
Run Code Online (Sandbox Code Playgroud)
这是该类的onCreateView方法
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
_fragmentView = inflater.inflate(R.layout.yes_fragment, container);
}
Run Code Online (Sandbox Code Playgroud)
这是片段XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,实现是空的,我没有在这段时间之前(而不是之后)实例化这个片段但是我得到这个错误:
E/AndroidRuntime(2995): FATAL EXCEPTION: main
E/AndroidRuntime(2995): java.lang.RuntimeException: Unable to start activity ComponentInfo{activities.MainActivity}:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Run Code Online (Sandbox Code Playgroud)
我认为它与操作栏中添加选项卡有关,因为当我注释掉actionBar.addTab()应用程序运行时(显然没有选项卡)
我真的想要提示如何解决这个问题.
谢谢你的头脑
第二个参数inflate()是默认父级:
_fragmentView = inflater.inflate(R.layout.yes_fragment, container);
Run Code Online (Sandbox Code Playgroud)
将此行更改为:
_fragmentView = inflater.inflate(R.layout.yes_fragment, null);
Run Code Online (Sandbox Code Playgroud)