2

我在创建一个类中有以下内容FragmentTabHost

public class TabsActivity extends FragmentActivity {
private FragmentTabHost mTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabs);
    mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    TabHost.TabSpec exploreSpec = mTabHost.newTabSpec("explore").setIndicator("Explore", getResources().getDrawable(R.drawable.exploreicon));
    mTabHost.addTab(exploreSpec);
}

}

我有一个单独的 .xml 文件用于设置主机,如 android 支持站点所示。这一切都在第二个活动中。我的主要活动有一组图像。单击一个会加载此活动。该应用程序最初运行。一旦我点击图像以加载此活动,尽管它会崩溃。在 LogCat 中我发现了以下错误:

java.lang.IllegalArgumentException: you must specify a way to create the tab content

我似乎在这个错误上找不到任何东西。

4

1 回答 1

2

如果无法查看您的 xml 文件 (R.layout.tabs),我会说某些设置不正确。

代替:

TabHost.TabSpec exploreSpec = mTabHost.newTabSpec("explore").setIndicator("Explore", getResources().getDrawable(R.drawable.exploreicon));
    mTabHost.addTab(exploreSpec);

尝试:

mTabHost.addTab(mTabHost.newTabSpec("explore").setIndicator("Explore", getResources().getDrawable(R.drawable.exploreicon)), TheAssociatedFragmentTab.class, null);

R.layout.tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff" >

    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >

        <FrameLayout
            android:id="@+id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

除此之外,您只需要一个 ExplorerFragment 类(扩展片段),它会覆盖 onCreateView 并像这样扩展您的资源管理器选项卡的布局..

ExplorerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
{
     super.onCreateView(inflater, container, savedInstanceState);
     return inflater.inflate(R.layout.explorer_fragment, container, false);
}

我没有尝试在我的选项卡上合并图像,但这段代码对我有用。让我知道这是否有帮助。

于 2013-10-11T18:41:20.690 回答