2012-04-01 102 views
0

我正在創建一個android應用程序(min sdk version 7或2.1及更高版本)。直到我決定我需要一個TabLayout,它一切正常。我添加了一個新類,並且擴展了TabActivity,但是當啓動AVD中的應用程序時,它崩潰並在日誌中聲明存在未捕獲的異常。而且它無法啓動該活動。Android TabLayout崩潰

ThermostatTabHost.java(注意,我沒加包/進口節省一些空間):

public class ThermostatTabHost extends TabActivity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.tabmain); 

     Resources res = getResources(); // Resource object to get Drawables 
     TabHost tabHost = getTabHost(); // The activity TabHost 
     TabHost.TabSpec spec; // Resusable TabSpec for each tab 
     Intent intent; // Reusable Intent for each tab 

     // Create an Intent to launch an Activity for the tab (to be reused) 
     intent = new Intent().setClass(this, ThermostatActivity.class); 

     // Initialize a TabSpec for each tab and add it to the TabHost 
     spec = tabHost.newTabSpec("Temperature").setIndicator("Program", 
          res.getDrawable(R.drawable.arrow_up)) 
         .setContent(intent); 
     tabHost.addTab(spec); 
    } 
} 

目前ThermostatActvity類唯一持有的onCreate函數,它們調用super.onCreate(savedInstance)並設置與textview的佈局。

的tabmain.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="5dp"> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="5dp" /> 
    </LinearLayout> 
</TabHost> 

最後清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.pack.thermostat" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="7" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".ThermostatTabHost" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

這個例子開門見山Android開發者網站(有一些小的改動),我也看了在類似的問題周圍,但我不能拿出任何東西。

預先感謝您。

+0

您可以發佈錯誤的logcat的輸出。 – flo 2012-04-01 17:10:39

回答

1

添加這種「ThermostatActivity」你manifeast文件

+0

謝謝,這確實是我忘了。我不知道我必須將所有使用的活動放在清單文件中。 – Zilarion 2012-04-01 17:11:16