我正在使用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;
}
}
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 :(得分: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");
答案 1 :(得分: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");