Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在片段中添加选项卡_Android_Tabs_Fragment - Fatal编程技术网

Android在片段中添加选项卡

Android在片段中添加选项卡,android,tabs,fragment,Android,Tabs,Fragment,我试图在片段中添加一个TabHost…下面给出了代码。在片段中,我试图添加TabHost以显示两个选项卡 public class TablesFragment extends Fragment { TabHost tabHost; TabHost.TabSpec spec; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance

我试图在片段中添加一个TabHost…下面给出了代码。在片段中,我试图添加TabHost以显示两个选项卡

public class TablesFragment extends Fragment {

TabHost tabHost;
TabHost.TabSpec spec;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.tables_fragment, container,
            false);
    tabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
    tabHost.setup();

    Intent intentAndroid = new Intent().setClass(getActivity(),
            NewsFragment.class);
    spec = tabHost.newTabSpec("Android").setContent(intentAndroid)
            .setIndicator("Android");
    tabHost.addTab(spec);

    Intent intentBus = new Intent().setClass(getActivity(), NewsFragment.class);
    spec = tabHost.newTabSpec("Welcome").setIndicator("Welcome")
            .setContent(intentBus).setIndicator("Welcome");
    tabHost.addTab(spec);
    return rootView;
}
}

这是给我的错误…需要助理与此代码

日志:

01-26 16:21:27.092: E/AndroidRuntime(5597): FATAL EXCEPTION: main
01-26 16:21:27.092: E/AndroidRuntime(5597): java.lang.IllegalStateException: Did you    forget to call 'public void setup(LocalActivityManager activityGroup)'?
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:692)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost.setCurrentTab(TabHost.java:358)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost.addTab(TabHost.java:236)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at com.fifaworldcup2014.TablesFragment.onCreateView(TablesFragment.java:31)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at com.fifaworldcup2014.MainActivity.onTabChanged(MainActivity.java:164)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:391)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost.setCurrentTab(TabHost.java:376)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.view.View.performClick(View.java:4084)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.view.View$PerformClick.run(View.java:16966)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.os.Handler.handleCallback(Handler.java:615)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.os.Looper.loop(Looper.java:137)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at java.lang.reflect.Method.invokeNative(Native Method)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at java.lang.reflect.Method.invoke(Method.java:511)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-26 16:21:27.092: E/AndroidRuntime(5597):     at dalvik.system.NativeStart.main(Native Method)

我相信,如果您的目标是API17+,这可能对您有用。 如果不是,你应该看看


签出下面的代码,如果你仍然在寻找答案,它肯定会对你有所帮助&对于旧版本,它将在API11上工作

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AnalogClock;
import android.widget.TabHost;

public class TabFragment extends Fragment {

    private TabHost mTabHost;
    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.tabinterview, container, false);
        mTabHost = (TabHost) rootView.findViewById(R.id.tabhost);
        mTabHost.setup();
        TabHost.TabSpec spec = mTabHost.newTabSpec("tag");
        spec.setIndicator("Android");
        spec.setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                // TODO Auto-generated method stub
                return (new AnalogClock(getActivity()));
            }
        });
        mTabHost.addTab(spec);
        spec = mTabHost.newTabSpec("tag1");
        spec.setIndicator("java");
        spec.setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                // TODO Auto-generated method stub
                return (new AnalogClock(getActivity()));
            }
        });
        mTabHost.addTab(spec);
        spec = mTabHost.newTabSpec("tag2");
        spec.setIndicator("Favourate");
        spec.setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                // TODO Auto-generated method stub
                return (new AnalogClock(getActivity()));
            }
        });
        mTabHost.addTab(spec);
        return rootView;
    }
}
如果您使用:

确保布局如下所示:

<FrameLayout 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"
tools:context=".view.user.DetailsFragment">

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">
    </TabWidget>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
    </FrameLayout>
</TabHost>


公共类MainActivity扩展了FragmentActivity实现了TabHost.OnTabChangeListenerAh,我的错误。您是否试图在MainActivity视图中设置“主”选项卡?或者片段内的选项卡控制器,用于其中一个片段内的选项卡式用户界面?如果MainActivity,我有选项卡,我尝试在其中有选项卡fragment@JonnyWright试着这样做:@cantonthinkofanythings是的,你可以用SimpleGetStureListener编程使它们可切换。布局是什么样子的?我知道这已经快一年了,但使用v22 compat库针对API17,并使用TabLayout,我已经能够在顶级活动上添加选项卡,向每个选项卡添加片段,然后将TabLayout添加到这些片段中,并获得第二级选项卡,然后将片段添加到这些片段中(嵌套)。将子片段添加到片段时必须使用。我会将此作为答案提交,但我没有代码示例,最终会被否决。。
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AnalogClock;
import android.widget.TabHost;

public class TabFragment extends Fragment {

    private TabHost mTabHost;
    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.tabinterview, container, false);
        mTabHost = (TabHost) rootView.findViewById(R.id.tabhost);
        mTabHost.setup();
        TabHost.TabSpec spec = mTabHost.newTabSpec("tag");
        spec.setIndicator("Android");
        spec.setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                // TODO Auto-generated method stub
                return (new AnalogClock(getActivity()));
            }
        });
        mTabHost.addTab(spec);
        spec = mTabHost.newTabSpec("tag1");
        spec.setIndicator("java");
        spec.setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                // TODO Auto-generated method stub
                return (new AnalogClock(getActivity()));
            }
        });
        mTabHost.addTab(spec);
        spec = mTabHost.newTabSpec("tag2");
        spec.setIndicator("Favourate");
        spec.setContent(new TabHost.TabContentFactory() {

            @Override
            public View createTabContent(String tag) {
                // TODO Auto-generated method stub
                return (new AnalogClock(getActivity()));
            }
        });
        mTabHost.addTab(spec);
        return rootView;
    }
}

this code how to give Page Link
<FrameLayout 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"
tools:context=".view.user.DetailsFragment">

<TabHost
    android:id="@+id/tabhost"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">
    </TabWidget>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">
    </FrameLayout>
</TabHost>