0

我正在尝试在我的 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);

可能的原因是什么?

4

3 回答 3

2

您的选项卡代码看起来不错,我的猜测是尝试获取视图NextActicvity时出现错误。TabHost

于 2012-07-06T20:07:36.027 回答
0

我也有同样的问题,当我在 Log 中看到错误是由于 null 返回:

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

然后你使用这个 nullTextView来分配颜色,所以它会给你 nullpointer 异常。

于 2013-09-19T06:45:10.787 回答
0

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

如何包括它们:

在应用程序标签内。

     <activity android:name="NextActivity"></activity>
于 2014-01-29T23:55:34.513 回答