作为初学者,我想学习使用 tabhost/tabwidget 的 android 选项卡式布局,尽管我已经阅读了一些已经弃用的帖子。所以...基本上,我想要实现的是有一个带有两个选项卡的布局,这两个选项卡都将显示一个具有不同内容的 web 视图。但是每次我尝试访问该布局时,我的应用程序都会崩溃。
这是它在我的 Logcat 中所说的:
日志猫:
04-09 22:30:53.628: E/AndroidRuntime(10406): FATAL EXCEPTION: main
04-09 22:30:53.628: E/AndroidRuntime(10406): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nicx.amazing/com.nicx.amazing.TabActivity}: java.lang.IllegalStateException: Already attached
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.access$600(ActivityThread.java:127)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.os.Looper.loop(Looper.java:137)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.main(ActivityThread.java:4448)
04-09 22:30:53.628: E/AndroidRuntime(10406): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 22:30:53.628: E/AndroidRuntime(10406): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 22:30:53.628: E/AndroidRuntime(10406): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
04-09 22:30:53.628: E/AndroidRuntime(10406): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
04-09 22:30:53.628: E/AndroidRuntime(10406): at dalvik.system.NativeStart.main(Native Method)
04-09 22:30:53.628: E/AndroidRuntime(10406): Caused by: java.lang.IllegalStateException: Already attached
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.support.v4.app.FragmentManagerImpl.attachActivity(FragmentManager.java:1844)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:193)
04-09 22:30:53.628: E/AndroidRuntime(10406): at com.nicx.amazing.TabActivity.onCreate(TabActivity.java:15)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.Activity.performCreate(Activity.java:4465)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-09 22:30:53.628: E/AndroidRuntime(10406): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
04-09 22:30:53.628: E/AndroidRuntime(10406): ... 11 more
这是我的 TabActivity.java 中的内容:
import android.os.Bundle;
import android.support.v4.app.*;
import android.widget.TabHost;
public class TabActivity extends FragmentActivity {
TabHost tHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tHost = (TabHost) findViewById(android.R.id.tabhost);
tHost.setup();
TabHost.TabSpec fb = tHost.newTabSpec("facebook");
fb.setIndicator("Facebook", null);
fb.setContent(new DummyTabContent(getBaseContext()));
tHost.addTab(fb);
TabHost.TabSpec tw = tHost.newTabSpec("twitter");
tw.setIndicator("Twitter", null);
tw.setContent(new DummyTabContent(getBaseContext()));
tHost.addTab(tw);
}
}
注意:我还没有弄清楚如何在我的选项卡中放置 web 视图,我只想首先查看选项卡的布局。
我的 activity_tab.xml 中也有这个:
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</TabHost>
我使用http://wptrafficanalyzer.in/blog/creating-navigation-tabs-using-tabhost-and-fragments-in-android/作为指南。我只是没有遵循它的片段,因为我没有使用它。你能帮我解决这个问题吗?