PreferenceActivity - > getActionBar()。setDisplayHomeAsUpEnabled给出NullPointerException

时间:2015-05-23 18:56:35

标签: android-activity android-theme preferenceactivity

我对讨论的话题有一些疑问。

我从以下代码中获取NullPointerException。我的问题是:

  1. 为什么我会收到NullPointerException?即为什么getActionBar()返回null?
  2. 请求答案为Layman条款。

    SettingActivity.java

    public class SettingsActivity extends PreferenceActivity {
    /**
     * Determines whether to always show the simplified settings UI, where
     * settings are presented in a single list. When false, settings are shown
     * as a master/detail two-pane view on tablets. When true, a single pane is
     * shown on tablets.
     */
    private static final boolean ALWAYS_SIMPLE_PREFS = false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setupActionBar();
    }
    
    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getSupportActionBar()
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
    

    Style.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>
    

    的build.gradle

    dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.android.support:support-v4:22.1.1'
    }
    

    错误

    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
    at com.example.deep.sunshine.SettingsActivity.setupActionBar(SettingsActivity.java:59)
    at com.example.deep.sunshine.SettingsActivity.onCreate(SettingsActivity.java:49)
    

0 个答案:

没有答案