如何在选项卡中添加片段

时间:2014-07-10 11:26:08

标签: android android-fragments

public class TabFragment扩展了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 QuestionFragment().getView());
        }
    });
    mTabHost.addTab(spec);

    return rootView;
}

}

在上面的代码我创建了选项卡,在内部选项卡中我必须添加相应的片段 我有结帐选项卡显示在片段,但当我尝试添加选项卡问题片段的设置内容时,我收到以下错误

07-10 11:00:26.030: E/AndroidRuntime(1025): FATAL EXCEPTION: main
07-10 11:00:26.030: E/AndroidRuntime(1025): java.lang.NullPointerException
07-10 11:00:26.030: E/AndroidRuntime(1025):     at android.widget.TabHost$FactoryContentStrategy.getContentView(TabHost.java:720)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at android.widget.TabHost.setCurrentTab(TabHost.java:413)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:154)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:546)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at android.view.View.performClick(View.java:4204)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at android.view.View$PerformClick.run(View.java:17355)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at android.os.Handler.handleCallback(Handler.java:725)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at android.os.Looper.loop(Looper.java:137)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 11:00:26.030: E/AndroidRuntime(1025):     at dalvik.system.NativeStart.main(Native Method)

请帮我在标签中添加片段或建议我任何其他逻辑

您的快速回复将受到高度赞赏

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找像这样的tabbar概念。

enter image description here

为此我可以使用以下链接。

https://github.com/JakeWharton/Android-ViewPagerIndicator

易于实施。
http://thorbek.net/online/2013/09/01/sliding-windows-with-tab-bar-in-android-activity/

http://blog.stylingandroid.com/archives/537

以下链接用于浏览指示器
http://viewpagerindicator.com/