android中的Null Pointer Exception,即使活动是在清单中声明的

时间:2014-02-09 18:24:44

标签: android nullpointerexception

描述:虽然我已经宣布了清单中的所有活动,但我仍然得到一个空指针异常

我该如何解决这个问题:

mycode的

MainActivity.java

public class MainActivity extends FragmentActivity implements TabListener  {

    ViewPager viewPager;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the view from main_fragment.xml
        setContentView(R.layout.main_activity);

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

        ActionBar.Tab tab1=actionBar.newTab();
        tab1.setText("Tab1");
        tab1.setTabListener(this);

        ActionBar.Tab tab2=actionBar.newTab();
        tab1.setText("Tab2");
        tab1.setTabListener(this);

        ActionBar.Tab tab3=actionBar.newTab();
        tab1.setText("Tab3");
        tab1.setTabListener(this);

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);

        }


    @Override
    protected void onDestroy() {
        Log.d(getClass().getName(), "[onDestroy(activity)]");
        super.onDestroy();
    }


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

    }


    @Override
    public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }


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

    }

}

清单

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="18" />
        <uses-permission android:name="android.permission.INTERNET"/>


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

</manifest>

日志

02-09 23:48:42.309: E/AndroidRuntime(1082): FATAL EXCEPTION: main
02-09 23:48:42.309: E/AndroidRuntime(1082): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.findmybuffet.restaurant_module/com.findmybuffet.restaurant_module.MainActivity}: java.lang.NullPointerException
02-09 23:48:42.309: E/AndroidRuntime(1082):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at android.os.Looper.loop(Looper.java:137)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at android.app.ActivityThread.main(ActivityThread.java:4340)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at java.lang.reflect.Method.invokeNative(Native Method)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at java.lang.reflect.Method.invoke(Method.java:511)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at dalvik.system.NativeStart.main(Native Method)
02-09 23:48:42.309: E/AndroidRuntime(1082): Caused by: java.lang.NullPointerException
02-09 23:48:42.309: E/AndroidRuntime(1082):     at com.findmybuffet.restaurant_module.MainActivity.onCreate(MainActivity.java:24)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at android.app.Activity.performCreate(Activity.java:4465)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-09 23:48:42.309: E/AndroidRuntime(1082):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-09 23:48:42.309: E/AndroidRuntime(1082):     ... 11 more

修改

MainActivity.java

public class MainActivity extends FragmentActivity implements TabListener  {

    ViewPager viewPager;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the view from main_fragment.xml
        setContentView(R.layout.main_activity);

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

        ActionBar.Tab tab1=actionBar.newTab();
        tab1.setText("Tab1");
        tab1.setTabListener(this);

        ActionBar.Tab tab2=actionBar.newTab();
        tab2.setText("Tab2");
        tab2.setTabListener(this);

        ActionBar.Tab tab3=actionBar.newTab();
        tab3.setText("Tab3");
        tab3.setTabListener(this);

        actionBar.addTab(tab1);
        actionBar.addTab(tab2);
        actionBar.addTab(tab3);

    }


    @Override
    protected void onDestroy() {
        Log.d(getClass().getName(), "[onDestroy(activity)]");
        super.onDestroy();
    }


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

    }


    @Override
    public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
        // TODO Auto-generated method stub

    }


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

    }

}

日志::

02-10 00:00:30.409: E/AndroidRuntime(1132): FATAL EXCEPTION: main
02-10 00:00:30.409: E/AndroidRuntime(1132): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.findmybuffet.restaurant_module/com.findmybuffet.restaurant_module.MainActivity}: java.lang.NullPointerException
02-10 00:00:30.409: E/AndroidRuntime(1132):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at android.os.Looper.loop(Looper.java:137)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at android.app.ActivityThread.main(ActivityThread.java:4340)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at java.lang.reflect.Method.invokeNative(Native Method)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at java.lang.reflect.Method.invoke(Method.java:511)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at dalvik.system.NativeStart.main(Native Method)
02-10 00:00:30.409: E/AndroidRuntime(1132): Caused by: java.lang.NullPointerException
02-10 00:00:30.409: E/AndroidRuntime(1132):     at com.findmybuffet.restaurant_module.MainActivity.onCreate(MainActivity.java:24)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at android.app.Activity.performCreate(Activity.java:4465)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-10 00:00:30.409: E/AndroidRuntime(1132):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-10 00:00:30.409: E/AndroidRuntime(1132):     ... 11 more

2 个答案:

答案 0 :(得分:2)

我认为你没有tab2tab3不为空。您已将tab1命名为tab2tab3。所以将其更改为此代码:

    tab1.setText("Tab1");
    tab1.setTabListener(this);

    ActionBar.Tab tab2=actionBar.newTab();
    tab2.setText("Tab2");
    tab2.setTabListener(this);

    ActionBar.Tab tab3=actionBar.newTab();
    tab3.setText("Tab3");
    tab3.setTabListener(this);

此外,在致电setContentView之前,您需要致电:

requestWindowFeature(Window.FEATURE_ACTION_BAR);

这会使您的getActionBar()来电不会返回空。

答案 1 :(得分:1)

看起来对getActionBar()的调用返回null。你应该检查你的风格/主题,并确保它不影响标题栏的显示。请参阅类似问题:getActionBar() returns null