本文介绍了获取空指针异常的getActionBar方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在操作栏使用的菜单项。但同时得到它在上创建方法,它给我的空指针异常。请帮忙
感谢
这里是onCreate方法code
// TODO自动生成方法存根
super.onCreate(paramBundle);
的setContentView(R.layout.activity_home);
最后的动作条localActionBar = getActionBar();
localActionBar.setNavigationMode(2);
this.mSectionsPagerAdapter =新SectionsPagerAdapter(getFragmentManager());
this.mViewPager =((ViewPager)findViewById(R.id.pager));
this.mViewPager.setAdapter(this.mSectionsPagerAdapter);
this.mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener()
{
公共无效onPageSelected(INT paramAnonymousInt)
{
localActionBar.setSelectedNavigationItem(paramAnonymousInt);
}
});
的for(int i = 0; I< this.mSectionsPagerAdapter.getCount();我++)
{
localActionBar.addTab(localActionBar.newTab().setText(this.mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
}
}
和日志猫误差
log01-15 13:37:09.092:E / AndroidRuntime(15604):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.chronos.cricketgraph / com.chronos。 cricketgraph.HomeActivity}:显示java.lang.NullPointerException
01-15 13:37:09.092:E / AndroidRuntime(15604):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
01-15 13:37:09.092:E / AndroidRuntime(15604):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
01-15 13:37:09.092:E / AndroidRuntime(15604):在android.app.ActivityThread.access $ 700(ActivityThread.java:143)
01-15 13:37:09.092:E / AndroidRuntime(15604):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1241)
01-15 13:37:09.092:E / AndroidRuntime(15604):在android.os.Handler.dispatchMessage(Handler.java:99)
01-15 13:37:09.092:E / AndroidRuntime(15604):在android.os.Looper.loop(Looper.java:137)
01-15 13:37:09.092:E / AndroidRuntime(15604):在android.app.ActivityThread.main(ActivityThread.java:4960)
01-15 13:37:09.092:E / AndroidRuntime(15604):在java.lang.reflect.Method.invokeNative(本机方法)
01-15 13:37:09.092:E / AndroidRuntime(15604):在java.lang.reflect.Method.invoke(Method.java:511)
01-15 13:37:09.092:E / AndroidRuntime(15604):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-15 13:37:09.092:E / AndroidRuntime(15604):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-15 13:37:09.092:E / AndroidRuntime(15604):在dalvik.system.NativeStart.main(本机方法)
01-15 13:37:09.092:E / AndroidRuntime(15604):由:显示java.lang.NullPointerException
01-15 13:37:09.092:E / AndroidRuntime(15604):在com.chronos.cricketgraph.HomeActivity.onCreate(HomeActivity.java:48)
01-15 13:37:09.092:E / AndroidRuntime(15604):在android.app.Activity.performCreate(Activity.java:5203)
01-15 13:37:09.092:E / AndroidRuntime(15604):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
解决方案
你延长ActionBarActivity?
公共类MainActivity扩展FragmentActivity实现ActionBar.TabListener {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
最后的动作条动作条= getActionBar();
actionBar.show();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
...
}
I have use menu items on the action bar. but while getting it in the on create method it giving me the null pointer exception. Please help
Thanks
here is the oncreate method code
// 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));
}
}
and the Log cat errors
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)
'
解决方案
Did you extend 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);
...
}
这篇关于获取空指针异常的getActionBar方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!