2013-04-09 75 views
0

作爲一個初學者,我想學習使用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/作爲指導。由於我沒有使用它,我只是沒有遵循它的片段。你能幫助我嗎?

+0

爲什麼是'機器人:layout_weight = 「0」'在? – 2013-04-09 14:43:10

+0

也刪除super.onCreate(savedInstanceState);因爲你稱它爲兩次 – 2013-04-09 14:43:52

+0

@TimCastelijns我只是遵循鏈接所說的內容,也是因爲每次我嘗試更改它時,該標籤都會垂直移動,而不是僅在頂部。 – Nicx 2013-04-09 14:52:54

回答

0

哦,親愛的,還必須有兩個活動代表Facebook和Twitter,你會把他們留下嗎?

您必須創建這些活動才能完成您的應用。

我發現你的TabActivity從FrameActivity擴展,這看起來不正確。 您的MainActivity應該擴展TabActivity。

這裏是我的

package com.izzyleung.mixedbartest; 

import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TabHost; 

@SuppressWarnings("deprecation") 
public class MainActivity extends TabActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    TabHost tabHost = getTabHost(); 

    Intent imageIntent = new Intent(); 
    imageIntent.setClass(this, ImageActivity.class); 
    TabHost.TabSpec imageSpec = tabHost.newTabSpec("Image"); 
    imageSpec.setIndicator("Image"); 
    imageSpec.setContent(imageIntent); 
    tabHost.addTab(imageSpec); 

    Intent notificationIntent = new Intent(); 
    notificationIntent.setClass(this, NotificationActivity.class); 
    TabHost.TabSpec notificationSpec = tabHost.newTabSpec("Notification"); 
    notificationSpec.setIndicator("Notification"); 
    notificationSpec.setContent(notificationIntent); 
    tabHost.addTab(notificationSpec); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.action_settings: 
     Intent settingsIntent = new Intent(); 
     settingsIntent.setClass(this, SettingsActivity.class); 
     startActivity(settingsIntent); 
     return true; 
    default: 
     return super.onOptionsItemSelected(item); 
    } 
    } 
} 

我的XML文件的examle是這樣的:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@android:id/tabhost" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".MainActivity" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:padding="5sp" > 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </TabWidget> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="5sp" > 
    </FrameLayout> 
</LinearLayout> 

+0

你太棒了!我終於明白了。我真的很困惑這些規格和意圖。非常感謝!現在我可以嘗試在webview上工作。我可以再問一個問題嗎?在意圖部分[imageIntent.setClass(this,ImageActivity.class);]中顯示的類是顯示標籤內容的方式嗎? – Nicx 2013-04-09 15:24:56

+0

是的,這是顯示標籤內容的方式。 :)我遇到了與處理TabActivity時遇到的相同的問題,並且我找到了解決方案,在Internet上解決它。谷歌現在建議我們這樣做Fragment,但是,我仍然試圖找出如何使用片段。 – 2013-04-10 02:37:12