如何在Android中使用TabHost Widget进行不同的活动?

时间:2014-02-22 06:20:15

标签: android android-activity tabs android-tabhost tabwidget

我正在开发一个应用程序。

哪个需要,活动根据Tab变化而变化。 我使用了以下代码并创建了两个活动。

StoryAlbum1.java StoryAlbum2.java

我也在manifest.xml文件中定义了两个。

但我收到了运行时错误。

我的代码是:

ListView list_stories;
TabHost tab_stories;
@Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.story_list);

            tab_stories=(TabHost)findViewById(R.id.tabhoststories);
            tab_stories.setup();

            setupTab1(new TextView(this), "Album 1");
            setupTab2(new TextView(this), "Album 2");
        }
        private void setupTab1(final View view, final String tag) 
        {

            View tabview = createTabView(tab_stories.getContext(), tag);

            Intent intent1 = new Intent().setClass(this, StoryAlbum1.class);
            TabSpec tab1 = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent1);

            tab_stories.addTab(tab1);
        }
        private void setupTab2(final View view, final String tag) 
        {

            View tabview = createTabView(tab_stories.getContext(), tag);

            Intent intent2 = new Intent().setClass(this, StoryAlbum2.class);
            TabSpec tab2 = tab_stories.newTabSpec(tag).setIndicator(tabview).setContent(intent2);

            tab_stories.addTab(tab2);
        }
private static View createTabView(final Context context, final String text) 
        {
            View view = LayoutInflater.from(context).inflate(R.layout.tabs_text, null);
            TextView tv = (TextView) view.findViewById(R.id.tabsText);
            tv.setText(text);
            return view;
        }
}

请帮我解决这个错误!

我的错误日志是:

02-22 06:03:58.247: E/AndroidRuntime(1191): FATAL EXCEPTION: main
02-22 06:03:58.247: E/AndroidRuntime(1191): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.videostreaming/com.example.videostreaming.Story_List}: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.os.Looper.loop(Looper.java:137)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at java.lang.reflect.Method.invoke(Method.java:511)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at dalvik.system.NativeStart.main(Native Method)
02-22 06:03:58.247: E/AndroidRuntime(1191): Caused by: java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:747)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.widget.TabHost.setCurrentTab(TabHost.java:413)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.widget.TabHost.addTab(TabHost.java:240)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at com.example.videostreaming.Story_List.setupTab1(Story_List.java:55)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at com.example.videostreaming.Story_List.onCreate(Story_List.java:38)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.app.Activity.performCreate(Activity.java:5104)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-22 06:03:58.247: E/AndroidRuntime(1191):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

谢谢。

1 个答案:

答案 0 :(得分:4)

您需要将MainActivity的基类从Activity更改为ActivityGroup,如下所示:

public class MainActivity extends ActivityGroup {
...
}

ActivityGroup将处理LocalActivityManager的一个实例。所以你不需要创建它。更改基类后,只需调用基类中定义的getLocalActivityManager()函数即可获取该实例。调用tabHost的设置功能如下:

 tabHost.setup(this.getLocalActivityManager());