Android FragmentTabHost:没有标签为null的标签

Bis*_*han 43 android android-fragments

我使用下面的代码,它不是渲染图形布局.显示错误为Exception raised during rendering: No tab known for tag null.

我怎么能解决这个问题?

<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>

</android.support.v4.app.FragmentTabHost>
Run Code Online (Sandbox Code Playgroud)

Ale*_*cha 8

这是我用来初始化TabHost的代码,它工作正常:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class DetailFragment extends Fragment {

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public DetailFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // R.layout.fragment_tabs_pager contains the layout as specified in your question
        View rootView = inflater.inflate(R.layout.fragment_tabs_pager, container, false);

        // Initialise the tab-host
        FragmentTabHost mTabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        // Initialise this list somewhere with the content that should be displayed
        List<String> itemsToBeDisplayed;

        for (String subItem : itemsToBeDisplayed) {
            // Give along the name - you can use this to hand over an ID for example
            Bundle b = new Bundle();
            b.putString("TAB_ITEM_NAME", subItem);

            // Add a tab to the tabHost
            mTabHost.addTab(mTabHost.newTabSpec(subItem).setIndicator(subItem), YourContentFragment.class, b);
        }
        return rootView;
    }
}
Run Code Online (Sandbox Code Playgroud)
/**
  * This class contains the actual content of a single tab  
  */
public class YourContentFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extras = getArguments();
        if (extras != null) {
            if (extras.containsKey("TAB_ITEM_NAME")) {
                String subItem = extras.getString("TAB_ITEM_NAME");
                // Do something with that string
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


use*_*049 6

似乎android支持库中存在一个错误.

我终于找到了这个解决方法:

将布局文件更改为:

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal" />

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后我通过代码FragmentTabHost创建了这个例子:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    tabHost = new FragmentTabHost(getActivity());
    inflater.inflate(R.layout.logs_view, tabHost);
    tabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);

    tabHost.addTab(tabHost.newTabSpec("simple").setIndicator("Simple"), ActivityLogFragment.class, null);
    tabHost.addTab(tabHost.newTabSpec("contacts").setIndicator("Contacts"), MiniStatementFragment.class, null);
    return tabHost;
}
Run Code Online (Sandbox Code Playgroud)


ism*_*lik 5

我遇到了这个问题,研究过,但找不到任何有效的解决方案.但是我注意到当我在'FragmentTabHost#setup'之后添加一个标签时,这个问题不会发生,但是例如,当我在'AsyncTask#onPostExecute'中添加一个标签时,就会出现这个问题.这看起来很愚蠢,但我试过,它是..基于那些,我找到了一个解决方案:

在'FragmentTabHost#setup'之后添加一个空标签,然后在任何地方添加其他标签,我尝试了这个解决方案,它的工作原理!我将在下面的布局和部分代码:

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:orientation="horizontal"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
    TabHost.TabSpec tabSpec = mTabHost.newTabSpec("TAB_FIRST").setIndicator("First");

    mTabHost.addTab(tabSpec, FirstFragment.class, null);

    TabWidget tabWidget = mTabHost.getTabWidget();
    if (tabWidget != null) {
        View tabWidgetChild = tabWidget.getChildAt(0);
        if (tabWidgetChild != null) {
            tabWidgetChild.setVisibility(View.GONE);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)