**MainActivity**
package com.example.tabtest;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class TabFragTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar
.newTab()
.setText("Tab1")
.setTabListener(
new TabListener<FragmentTab1>(this, "tab1",
FragmentTab1.class)));
actionBar.addTab(actionBar
.newTab()
.setText("Tab2")
.setTabListener(
new TabListener<FragmentTab2>(this, "tab3",
FragmentTab2.class)));
actionBar.addTab(actionBar
.newTab()
.setText("Tab3")
.setTabListener(
new TabListener<FragmentTab3>(this, "tab3",
FragmentTab3.class)));
if (savedInstanceState != null) {
actionBar.setSelectedNavigationItem(savedInstanceState
.getInt("selectedTab", 0));
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("selectedTab", getActionBar()
.getSelectedNavigationIndex());
}
}
**TabListener**
package com.example.tabtest;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ActionBar.Tab;
import android.widget.Toast;
public class TabListener<T extends Fragment> implements ActionBar.TabListener {
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
private Fragment mFragment;
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
FragmentTransaction fragmentTransaction = mActivity
.getFragmentManager().beginTransaction();
fragmentTransaction.detach(mFragment);
fragmentTransaction.commit();
}
}
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(),
null);
fragmentTransaction
.add(android.R.id.content, mFragment, mTag);
} else {
fragmentTransaction.attach(mFragment);
}
}
public void onTabUnselected(Tab tab,
FragmentTransaction fragmentTransaction) {
if (mFragment != null) {
fragmentTransaction.detach(mFragment);
}
}
public void onTabReselected(Tab tab,
FragmentTransaction fragmentTransaction) {
Toast.makeText(mActivity, "onTabReselected!", Toast.LENGTH_SHORT)
.show();
}
}
**tabview1**
package com.example.tabtest;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentTab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.simple, container, false);
View tv = v.findViewById(R.id.text);
((TextView) tv).setText("1");
return v;
}
}
**tabview2**
package com.example.tabtest;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentTab2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.simple, container, false);
View tv = v.findViewById(R.id.text);
((TextView) tv).setText("2.");
return v;
}
}
**tabview3**
package com.example.tabtest;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentTab3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.simple, container, false);
View tv = v.findViewById(R.id.text);
((TextView) tv).setText("3");
return v;
}
}
**xml**
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TabFragTest" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
}
**manifest**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tabtest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
// android:theme="@android:style/Theme.NoTitleBar"
>
<activity
android:name="com.example.tabtest.TabFragTest"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
日志猫
07-09 09:32:29.953:D/gralloc_goldfish(770):没有检测到 GPU 仿真的仿真器。07-09 09:59:45.604:E/Trace(819):错误打开跟踪文件:没有这样的文件或目录 (2) 07-09 09:59:46.003:D/AndroidRuntime(819):关闭 VM 07- 09 09:59:46.003: W/dalvikvm(819): threadid=1: 线程退出未捕获异常 (group=0x40a13300) 07-09 09:59:46.013: E/AndroidRuntime(819): 致命异常: main 07- 09 09:59:46.013: E/AndroidRuntime(819): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.tabtest/com.example.tabtest.TabFragTest}: java.lang.NullPointerException 07-09 09 :59:46.013: E/AndroidRuntime(819): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 07-09 09:59:46.013: E/AndroidRuntime(819): 在 android.app.ActivityThread。 handleLaunchActivity(ActivityThread.java:2084)07-09 09:59:46。
请帮我