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



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

这需要,活动会根据选项卡的变化而变化。我使用了以下代码并创建了两个活动。

故事专辑1.java故事专辑2.java

我也在清单.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)

谢谢。

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

public class MainActivity extends ActivityGroup {
...
}

ActivityGroup 将负责 LocalActivityManager 的一个实例。因此,您无需创建它。更改基类后,只需调用基类中定义的 getLocalActivityManager() 函数即可获取该实例。调用 tabHost 的设置函数,如下所示:

 tabHost.setup(this.getLocalActivityManager());

相关内容

  • 没有找到相关文章

最新更新