在getActionBar方法中获取空指针异常

时间:2015-01-15 08:14:11

标签: android android-activity

我在操作栏上使用了菜单项。但是在创建方法中获取它时,它给了我空指针异常。请帮忙

由于

这里是oncreate方法代码

    // TODO Auto-generated method stub
    super.onCreate(paramBundle);
    setContentView(R.layout.activity_home);


     final ActionBar localActionBar=getActionBar();
     localActionBar.setNavigationMode(2);
     this.mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
     this.mViewPager = ((ViewPager)findViewById(R.id.pager));
     this.mViewPager.setAdapter(this.mSectionsPagerAdapter);
        this.mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
        {
             public void onPageSelected(int paramAnonymousInt)
             {
               localActionBar.setSelectedNavigationItem(paramAnonymousInt);
             }
           });
        for (int i = 0; i < this.mSectionsPagerAdapter.getCount(); i++) 
        {
            localActionBar.addTab(localActionBar.newTab().setText(this.mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
        }

}

和Log cat错误

log01-15 13:37:09.092: E/AndroidRuntime(15604): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chronos.cricketgraph/com.chronos.cricketgraph.HomeActivity}: java.lang.NullPointerException
01-15 13:37:09.092: E/AndroidRuntime(15604):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at android.app.ActivityThread.access$700(ActivityThread.java:143)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at android.os.Looper.loop(Looper.java:137)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at android.app.ActivityThread.main(ActivityThread.java:4960)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at java.lang.reflect.Method.invokeNative(Native Method)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at java.lang.reflect.Method.invoke(Method.java:511)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at dalvik.system.NativeStart.main(Native Method)
01-15 13:37:09.092: E/AndroidRuntime(15604): Caused by: java.lang.NullPointerException
01-15 13:37:09.092: E/AndroidRuntime(15604):    at com.chronos.cricketgraph.HomeActivity.onCreate(HomeActivity.java:48)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at android.app.Activity.performCreate(Activity.java:5203)
01-15 13:37:09.092: E/AndroidRuntime(15604):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
'

5 个答案:

答案 0 :(得分:1)

如果您使用Theme.AppCompat,请展开ActionBarActivity。也可以使用getSupportActionBar()代替getActionBar()。您可能还需要启用标题(但我不确定这是否适用于所有Android版本) - 因此应删除requestWindowFeature(Window.FEATURE_NO_TITLE); - 您的主题是否有:

<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>

任何机会。

答案 1 :(得分:1)

为提供AcionBar的应用程序使用主题,也许您可​​以包含appcompact支持库。

您可以查看this post

答案 2 :(得分:1)

您是否扩展了ActionBarActivity?

public class MainActivity extends FragmentActivity implements ActionBar.TabListener{


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final ActionBar actionBar = getActionBar();
    actionBar.show(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ...
}

答案 3 :(得分:0)

取决于您的版本(在Manifest中),您需要使用getSupportActionBar();getActionBar();。您还确定homeActivyActivity.class扩展了什么?

答案 4 :(得分:0)

尝试使用getSupportedActionBar-Method。我想你使用另一个版本的android