无法启动活动ComponentInfo java.lang.NullPointerException

时间:2014-07-30 07:37:22

标签: android

我在MainActivity.java中得到一个空指针Exception。我在指定的行中提到了错误。

StackTrace:

E/AndroidRuntime(1250): FATAL EXCEPTION: main
E/AndroidRuntime(1250): Process: com.actionbartabs, PID: 1250
E/AndroidRuntime(1250): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.actionbartabs/com.actionbartabs.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(1250):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
E/AndroidRuntime(1250):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
E/AndroidRuntime(1250):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
E/AndroidRuntime(1250):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
E/AndroidRuntime(1250):     at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(1250):     at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(1250):     at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(1250):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1250):     at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1250):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(1250):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(1250):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1250): Caused by: java.lang.NullPointerException
E/AndroidRuntime(1250):     at com.actionbartabs.MainActivity.onCreate(MainActivity.java:34)
E/AndroidRuntime(1250):     at android.app.Activity.performCreate(Activity.java:5231)
E/AndroidRuntime(1250):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(1250):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
E/AndroidRuntime(1250):     ... 11 more

MainActivity.java:

import com.actionbartabs.adapter.TabsPagerAdapter;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;


@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements ActionBar.TabListener{

    private ViewPager viewPager;
    private TabsPagerAdapter tabsPagerAdapter;
    private ActionBar actionBar;

    private String[] tabs={"Top_Rated","Games","Movies"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager=(ViewPager)findViewById(R.id.pager);
        actionBar=getActionBar();
        tabsPagerAdapter=new TabsPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(tabsPagerAdapter);
        actionBar.setHomeButtonEnabled(false); -----> Error occurred here
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        for(String tab_name:tabs){
            actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));

        }

}

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {

    }

}

我需要摆脱那个错误。任何人都可以帮助我。感谢你。

2 个答案:

答案 0 :(得分:2)

你真正的问题在于:

actionBar = getActionBar(); //This returns null.

你的主题中是否有一个ActionBar定义,如果是可见的话?

如果这些都不是您的问题,我建议您查看此问题getActionBar() returns null

答案 1 :(得分:0)

android:theme="@android:style/Theme.Holo.Light.DarkActionBar"中的元素中添加了这些manifest解决了问题。