听ActionBar选项卡时的NPE?

时间:2014-06-24 13:51:35

标签: android android-actionbar android-actionbaractivity

我有一个mainActivity,其中包含五个AtctionBarTabs,每个标签都由Fragment的布局表示,如下面的代码所示。在运行时,应用程序崩溃NPE。请查看代码并告诉我缺少什么?

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar mActionBar = getActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    ActionBar.Tab tab1 = mActionBar.newTab().setText("Tab 1");
    ActionBar.Tab tab2 = mActionBar.newTab().setText("Tab 2");
    ActionBar.Tab tab3 = mActionBar.newTab().setText("Tab 3");
    ActionBar.Tab tab4 = mActionBar.newTab().setText("Tab 4");
    ActionBar.Tab tab5 = mActionBar.newTab().setText("Tab 5");

    Fragment frag1 = new Fragment1();
    Fragment frag2 = new Fragment2();
    Fragment frag3 = new Fragment3();
    Fragment frag4 = new Fragment4();
    Fragment frag5 = new Fragment5();

    tab1.setTabListener(new mTabListener (getApplicationContext(), frag1));
    tab2.setTabListener(new mTabListener (getApplicationContext(), frag2));
    tab3.setTabListener(new mTabListener (getApplicationContext(), frag3));
    tab4.setTabListener(new mTabListener (getApplicationContext(), frag4));
    tab5.setTabListener(new mTabListener (getApplicationContext(), frag5));     

    mActionBar.addTab(tab1);
    mActionBar.addTab(tab2);
    mActionBar.addTab(tab3);
    mActionBar.addTab(tab4);
    mActionBar.addTab(tab5);
}

class mTabListener implements ActionBar.TabListener {

    private Context context;
    private Fragment frag;

    public mTabListener (Context context, Fragment frag) {
        this.context = context;
        this.frag = frag;
    }

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
        arg1.replace(R.id.fragment_container, this.frag);
    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }

}

}

logcat的

06-24 16:37:35.502: E/AndroidRuntime(16052): FATAL EXCEPTION: main
06-24 16:37:35.502: E/AndroidRuntime(16052): Process: com.example.actionbartabs00, PID:   
16052
06-24 16:37:35.502: E/AndroidRuntime(16052): java.lang.RuntimeException: Unable to  
start activity   
ComponentInfo{com.example.actionbartabs00/com.example.actionbartabs00.MainActivity}: 
java.lang.NullPointerException
06-24 16:37:35.502: E/AndroidRuntime(16052):    at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at   
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2340)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at  
android.app.ActivityThread.access$800(ActivityThread.java:157)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at    
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at   
android.os.Handler.dispatchMessage(Handler.java:102)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at   
android.os.Looper.loop(Looper.java:157)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at   
android.app.ActivityThread.main(ActivityThread.java:5293)
06-2 4 16:37:35.502: E/AndroidRuntime(16052):   at   
java.lang.reflect.Method.invokeNative(Native Method)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at   
java.lang.reflect.Method.invoke(Method.java:515)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at  
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at   
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at 
dalvik.system.NativeStart.main(Native Method)
06-24 16:37:35.502: E/AndroidRuntime(16052): Caused by: java.lang.NullPointerException
06-24 16:37:35.502: E/AndroidRuntime(16052):    at  
com.example.actionbartabs00.MainActivity.onCreate(MainActivity.java:19)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at 
android.app.Activity.performCreate(Activity.java:5389)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at   
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
06-24 16:37:35.502: E/AndroidRuntime(16052):    at   
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246)
06-24 16:37:35.502: E/AndroidRuntime(16052):    ... 11 more

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.actionbartabs00"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="19"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.actionbartabs00.MainActivity"
        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>

<resources>

<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

0 个答案:

没有答案