Android:tabHost.addHost(...)应用程序在tabHost.addTab崩溃

时间:2012-07-06 14:49:51

标签: android android-tabhost

我正在尝试在我的android 2.3.3中添加标签主机。这是布局目录下的tab.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost">

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

    <TabWidget 
        android:id="@android:id/tabs"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:layout_marginBottom="5dip">
    </TabWidget>

    <FrameLayout 
        android:id="@android:id/tabcontent"
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"
        android:background="#000000">
    </FrameLayout>
</LinearLayout>

这是我的TabActivity类

package com.tabtest.tab
import android.app.AlertDialog;
import android.app.TabActivity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

public class MyTabActivity extends TabActivity implements OnTabChangeListener {
TabHost tabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab);

    if(isOnline()){
        /* TabHost will have Tabs */
        tabHost = getTabHost();
        tabHost.setOnTabChangedListener(this);

        /* tid1 is firstTabSpec Id. Its used to access outside. */
        TabSpec firstTabSpec = tabHost.newTabSpec("tid1");

        /* TabSpec setIndicator() is used to set name for the tab. */
        /* TabSpec setContent() is used to set content for a particular tab. */
        firstTabSpec.setIndicator("1st tab", getResources().getDrawable(R.drawable.tabicon));

        firstTabSpec.setContent(new Intent(this, NextActivity.class));

        /* Add tabSpec to the TabHost to display. */
        tabHost.addTab(firstTabSpec);

        for(int i=0; i<tabHost.getTabWidget().getChildCount(); i++)
        {
            tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#0066a4"));
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
            tv.setTextColor(Color.WHITE);
        }

        tabHost.getTabWidget().setCurrentTab(0);
        tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#0780c9"));
        TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
        tv.setTextColor(Color.BLACK);
    }
    else
    {
        Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Internet Connectivity");
        builder.setMessage("Sorry, no active internet connection found. Please connect to internet.");
        builder.setPositiveButton("OK", null);
        builder.show();
    }
}

public void onTabChanged(String tabId) {
    for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
    {
        tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#0066a4"));
        TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
        tv.setTextColor(Color.WHITE);
    } 

    tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0780c9"));
    TextView tv = (TextView) tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).findViewById(android.R.id.title);
    tv.setTextColor(Color.BLACK);
}

public boolean isOnline() {
    ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    //mobile
    State mobile = conMan.getNetworkInfo(0).getState();
    //wifi
    State wifi = conMan.getNetworkInfo(1).getState();
    if (mobile == NetworkInfo.State.CONNECTED || mobile == NetworkInfo.State.CONNECTING || wifi == NetworkInfo.State.CONNECTED || wifi == NetworkInfo.State.CONNECTING) {
        return true;
    } else {
        return false;
    }

}
}

此应用程序崩溃“应用程序TabTest(进程com.tabtest.tab)意外停止。请再试一次。”我调试了代码,然后崩溃了

tabHost.addTab(firstTabSpec);

可能的原因是什么?

3 个答案:

答案 0 :(得分:2)

您的标签代码看起来不错,我的猜测是,当NextActicvity尝试获取视图时,TabHost中的某些内容会出错。

答案 1 :(得分:0)

我也有同样的问题,当我在Log中看到错误归因于null返回的点时:

TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);

然后使用此null TextView来指定颜色,以便它为您提供nullpointer异常。

答案 2 :(得分:0)

之前我遇到过同样的问题......所以在网上进行调试和搜索之后。我知道我忘记将它们包含在manifest.xml中

如何包含它们:

在应用程序标记内。

     <activity android:name="NextActivity"></activity>