getActionBar返回NPE

时间:2016-02-09 14:39:19

标签: android android-fragments android-actionbar android-viewpager

我有一个带有操作栏的主要活动。在运行时,尽管actionbar对象初始化为getActionBar(),我收到带有logcat的NPE 输出

主要活动

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

3 个答案:

答案 0 :(得分:1)

而不是FragmentActivity的延伸,只需从AppCompatActivity延伸,然后拨打getSupportActionBar()而不是getActionBar()

答案 1 :(得分:0)

使用support:appcompat库来使用ToolBar / ActionBar。

AppCompat(又名ActionBarCompat)开始作为运行在Gingerbread上的设备的Android 4.0 ActionBar API的后端,在后端实现和框架实现之上提供了一个通用的API层。

使用AppCompatActivity代替FragmentActivity

getSupportActionBar()代替getActionBar()

有关AppCompat的更多信息,关于它,这里很不错article

答案 2 :(得分:0)

导入ActionBar import android.support.v7.app.ActionBar;AppCompatActivity

扩展MainActivity

然后:

mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  //but this is deprecated