簡體   English   中英

Android TabLayout崩潰

[英]Android TabLayout crashing

我自己創建了一個Android應用(最低SDK版本7或2.1及更高版本)。 一切都很好,直到我決定需要一個TabLayout。 我添加了一個新類,並使其擴展了TabActivity,但是在AVD中啟動該應用程序時,它崩潰了,並在日志中指出了未捕獲的異常。 而且它無法啟動活動。

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開發人員網站(有一些小的更改),我也到處尋找類似的問題,但是我什么都沒想。

先感謝您。

在清單文件中添加此“ ThermostatActivity”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM