Reputation: 168
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)
'
Upvotes: 3
Views: 2053
Reputation: 21
Use a theme for your application that provides the AcionBar
, maybe you can include the appcompact
support library.
You can give a look at this post:
Upvotes: 1
Reputation: 661
Try to use the getSupportedActionBar-Method. I think you use another version of android
Upvotes: 0
Reputation: 4066
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);
...
}
Upvotes: 1
Reputation: 261
Depends of your version (in Manifest) you need to use getSupportActionBar();
or getActionBar();
. Also are you sure what your homeActivy
are extending from an Activity.class
?
Upvotes: 0
Reputation: 49986
If you use Theme.AppCompat
, then extend ActionBarActivity
. Also use getSupportActionBar()
instead of getActionBar()
. You might also need to enable title (but I am not sure if thats true for all android versions) - so requestWindowFeature(Window.FEATURE_NO_TITLE);
should be removed - do you have in your theme:
<item name="windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
by any chance.
Upvotes: 1