2013-05-02 129 views
1

我正在使用TabHost創建一個帶有3個選項卡的簡單標籤界面。當我運行我的應用程序有錯誤,調試器顯示以下內容:Android TabHost導致運行時錯誤

android runtime error

這看起來奇怪,因爲ActivityThread是Android的源的一部分。我曾在StackOverflow上尋找類似的問題,但他們沒有幫助。我很困惑,我會很感激你的幫助。

這裏是我的代碼:

標記:

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

    <TabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" > 

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

      <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" > 

       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:id="@+id/tab1Layout" 
        android:orientation="vertical"> 

        <TextView 
         android:id="@+id/tab1_text" 
         android:text="test tab 1" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 
        </TextView> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:id="@+id/tab2Layout" 
        android:orientation="vertical"> 

        <TextView 
         android:id="@+id/tab2_text" 
         android:text="test tab 2" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 
        </TextView> 
       </LinearLayout> 

       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:id="@+id/tab3Layout" 
        android:orientation="vertical"> 

        <TextView 
         android:id="@+id/tab3_text" 
         android:text="test tab 3" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 
        </TextView> 
       </LinearLayout> 
      </FrameLayout> 
     </LinearLayout> 

    </TabHost> 

</RelativeLayout> 

代碼:

package com.company.test; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 

public class MainActivity extends Activity { 

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

     // test tabs functionality 
     TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
     tabHost.setup(); 

     TabSpec resultsAll = tabHost.newTabSpec("tab1"); 
     resultsAll 
      .setContent(R.id.tab1Layout) 
      .setIndicator("All"); 

     TabSpec resultsWords = tabHost.newTabSpec("tab2"); 
     resultsAll 
      .setContent(R.id.tab2Layout) 
      .setIndicator("Words"); 

     TabSpec resultsTerms = tabHost.newTabSpec("tab3"); 
     resultsAll 
      .setContent(R.id.tab3Layout) 
      .setIndicator("Terms"); 

     tabHost.addTab(resultsAll); 
     tabHost.addTab(resultsWords); 
     tabHost.addTab(resultsTerms); 

     tabHost.setCurrentTab(0); 
    } 

    @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; 
    } 

} 

的logcat:

05-02 10:02:13.024: E/AndroidRuntime(79): Error reporting WTF 
05-02 10:02:13.024: E/AndroidRuntime(79): java.lang.NullPointerException 
05-02 10:02:13.024: E/AndroidRuntime(79): at com.android.internal.os.RuntimeInit.wtf(RuntimeInit.java:345) 
05-02 10:02:13.024: E/AndroidRuntime(79): at android.util.Log$1.onTerribleFailure(Log.java:103) 
05-02 10:02:13.024: E/AndroidRuntime(79): at android.util.Log.wtf(Log.java:278) 
05-02 10:02:13.024: E/AndroidRuntime(79): at com.android.internal.os.BatteryStatsImpl.getNetworkStatsDetailGroupedByUid(BatteryStatsImpl.java:5738) 
05-02 10:02:13.024: E/AndroidRuntime(79): at com.android.internal.os.BatteryStatsImpl.access$100(BatteryStatsImpl.java:76) 
05-02 10:02:13.024: E/AndroidRuntime(79): at com.android.internal.os.BatteryStatsImpl$Uid.computeCurrentTcpBytesReceived(BatteryStatsImpl.java:2457) 
05-02 10:02:13.024: E/AndroidRuntime(79): at com.android.internal.os.BatteryStatsImpl$Uid.getTcpBytesReceived(BatteryStatsImpl.java:2446) 
05-02 10:02:13.024: E/AndroidRuntime(79): at com.android.internal.os.BatteryStatsImpl.writeSummaryToParcel(BatteryStatsImpl.java:5437) 
05-02 10:02:13.024: E/AndroidRuntime(79): at com.android.internal.os.BatteryStatsImpl.writeLocked(BatteryStatsImpl.java:4836) 
05-02 10:02:13.024: E/AndroidRuntime(79): at com.android.internal.os.BatteryStatsImpl.writeAsyncLocked(BatteryStatsImpl.java:4818) 
05-02 10:02:13.024: E/AndroidRuntime(79): at com.android.server.am.ActivityManagerService.<init>(ActivityManagerService.java:1492) 
05-02 10:02:13.024: E/AndroidRuntime(79): at com.android.server.am.ActivityManagerService.<init>(ActivityManagerService.java:151) 
05-02 10:02:13.024: E/AndroidRuntime(79): at com.android.server.am.ActivityManagerService$AThread.run(ActivityManagerService.java:1393) 
+0

除了使用'TabHost tabHost =(TabHost)findViewById(android.R.id.tabhost);'只是嘗試用'TabHost tabHost = getTabHost();' – GrIsHu 2013-05-02 07:50:33

+0

@GrIsHu這億韓元不起作用,因爲我沒有擴展已棄用的TabActivity類。 – noname 2013-05-02 08:09:55

+0

它與om.android.internal.os.BatteryStatsImpl.getNetworkStatsDetailGroupedByUid()有什麼關係? – 2013-10-24 14:15:53

回答

2

resultsWords和resultsTerms設置不正確。您在resultsAll上使用setContent(...)和setIndicator(...)。你可以試試:

TabSpec resultsAll = tabHost.newTabSpec("tab1"); 
    resultsAll 
     .setContent(R.id.tab1Layout) 
     .setIndicator("All"); 

    TabSpec resultsWords = tabHost.newTabSpec("tab2"); 
    resultsWords 
     .setContent(R.id.tab2Layout) 
     .setIndicator("Words"); 

    TabSpec resultsTerms = tabHost.newTabSpec("tab3"); 
    resultsTerms 
     .setContent(R.id.tab3Layout) 
     .setIndicator("Terms"); 
+0

男人,這是一個愚蠢的錯誤。多麼可怕的錯字!非常感謝你。新鮮的外觀幫助。 – noname 2013-05-02 07:55:42

1

你錯誤地提到你的每一TabSpec對象在代碼中添加標籤。只需將其更改如下:

將每個TabSpec的實例從resultsAll更改爲resultsWords,resultsTerms

TabSpec resultsAll = tabHost.newTabSpec("tab1"); 
    resultsAll 
    .setContent(R.id.tab1Layout) 
    .setIndicator("All"); 
TabSpec resultsWords = tabHost.newTabSpec("tab2"); 
    resultsWords 
    .setContent(R.id.tab2Layout) 
    .setIndicator("Words"); 
TabSpec resultsTerms = tabHost.newTabSpec("tab3"); 
resultsTerms 
    .setContent(R.id.tab3Layout) 
    .setIndicator("Terms");