我正在尝试在我的 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);
可能的原因是什么?