0

I have a main activity with actionbar. at run time, despite the actionbar object is initialised to getActionBar(), i receive NPE with the logcat output below

main activity:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

private ViewPager mViewPager;
private MyTabsPagerAdapter mPagerAdapter;
private ActionBar mActionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    List<Fragment> mFragList = new ArrayList<Fragment>();
    mFragList.add(new Aufgabe_1());
    mFragList.add(new Fragment02());
    mFragList.add(new Fragment03());

    mViewPager = (ViewPager) findViewById(R.id.pager);
    mActionBar = getActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mPagerAdapter = new MyTabsPagerAdapter(getSupportFragmentManager(), mFragList);
    mViewPager.setAdapter(mPagerAdapter);

    for(int i=0; i<mFragList.size(); i++) {
        mActionBar.addTab(mActionBar.newTab().setText("Aufgabe_"+(i+1)).setTabListener(this));          
    }

    mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            mActionBar.setSelectedNavigationItem(arg0);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    });

}

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

}

@Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
    // TODO Auto-generated method stub
    mViewPager.setCurrentItem(arg0.getPosition());
    //mActionBar.setSelectedNavigationItem(arg0.getPosition());
}

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

}
}

logcat:

02-09 15:34:35.080 26079-26079/? E/AndroidRuntime: FATAL EXCEPTION: main
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime: Process: com.example.com.myapplication, PID: 26079
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.myapplication/com.example.com.vpager_02.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:177)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5938)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setNavigationMode(int)' on a null object reference
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at com.example.com.vpager_02.MainActivity.onCreate(MainActivity.java:33)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6288)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2642)
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754) 
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:177) 
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5938) 
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
02-09 15:34:35.080 26079-26079/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 
02-09 15:34:35.080 795-1150/? V/ApplicationPolicy: isApplicationStateBlocked userId 0 pkgname com.example.com.myapplication
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Can you please add the imports too and tell us what the styles are? perhaps you missed something, btw, it should be fixed with `getSupportActionBar();` – ʍѳђઽ૯ท Feb 09 '16 at 15:06
  • Possible duplicate of [Android Error \[Attempt to invoke virtual method 'void android.app.ActionBar' on a null object reference\]](http://stackoverflow.com/questions/28144657/android-error-attempt-to-invoke-virtual-method-void-android-app-actionbar-on) – ʍѳђઽ૯ท Feb 09 '16 at 15:08

3 Answers3

1

Instead of extends from FragmentActivity just extends form AppCompatActivity and call getSupportActionBar() instead of getActionBar()

Gueorgui Obregon
  • 5,077
  • 3
  • 33
  • 57
0

use support:appcompat library to use ToolBar/ActionBar.

AppCompat (aka ActionBarCompat) started out as a backport of the Android 4.0 ActionBar API for devices running on Gingerbread, providing a common API layer on top of the backported implementation and the framework implementation.

use AppCompatActivity instead of FragmentActivity

getSupportActionBar() instead of getActionBar()


more about AppCompat, here is nice article about it.

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

Import ActionBar import android.support.v7.app.ActionBar; Extend MainActivity from AppCompatActivity

Then:

mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  //but this is deprecated
daniel.keresztes
  • 875
  • 2
  • 15
  • 25