作爲一個初學者,我想學習使用tabhost/tabwidget的android的選項卡式佈局,儘管我已經閱讀了一些已經廢棄的帖子。所以......基本上,我試圖實現的是具有兩個選項卡的佈局,它們都將顯示具有不同內容的webview。但每次嘗試訪問該佈局時,我的應用都會崩潰。2標籤不工作,崩潰應用
下面是它在我的logcat說:
的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);
}
}
注:我還沒有想出如何在我的標籤中放置一個webview,我只想首先看到我的標籤佈局。
我也有這個在我的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/作爲指導。由於我沒有使用它,我只是沒有遵循它的片段。你能幫助我嗎?
爲什麼是'機器人:layout_weight = 「0」'在? –
2013-04-09 14:43:10
也刪除super.onCreate(savedInstanceState);因爲你稱它爲兩次 – 2013-04-09 14:43:52
@TimCastelijns我只是遵循鏈接所說的內容,也是因爲每次我嘗試更改它時,該標籤都會垂直移動,而不是僅在頂部。 – Nicx 2013-04-09 14:52:54