1

我正在使用 TabHost 创建一个带有 3 个选项卡的简单选项卡界面。当我运行我的应用程序时出现错误,调试器显示以下内容:

安卓运行时错误

这看起来很奇怪,因为 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;
    }

}

日志猫:

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)
4

2 回答 2

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");
于 2013-05-02T07:49:05.603 回答
1

您错误地引用了每个TabSpec对象以在代码中添加选项卡。只需将其更改如下:

将每个实例TabSpec从更改resultsAllresultsWords, 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");
于 2013-05-02T07:56:30.063 回答