简体   繁体   English

Android设置页面未显示任何首选项

[英]Android settings page not showing any preferences

Background 背景

In a rather simple Android App a Settings Activity is used for just one ListPreference . 在一个相当简单的Android应用程序中,“设置活动”仅用于一个ListPreference When clicking the menu item in the header a new screen shows up without any preference or header. 单击标题中的菜单项时,将显示一个新屏幕,没有任何首选项或标题。

  • compiledSdkVersion & targetSdkVersion = 25 编译的SdkVersion和targetSdkVersion = 25
  • minSdkVersion = 10 minSdkVersion = 10

Code

Here is the code that I currently have. 这是我当前拥有的代码。

SettingsActivity.java: Here the onCreate method is never called. SettingsActivity.java:永远不会调用onCreate方法。

public class SettingsActivity extends PreferenceActivity implements Preference.OnPreferenceChangeListener {

    /**
     * {@link PreferenceActivity#onCreate(Bundle)}
     */
    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);

        addPreferencesFromResource(R.xml.pref_movies);
    }

    /**
     * {@link android.preference.Preference.OnPreferenceChangeListener#onPreferenceChange(Preference, Object)}
     */
    @Override
    public boolean onPreferenceChange(Preference preference, Object o) {

        return false;
    }
}

pref_movies.xml: Is located in res/xml . pref_movies.xml:位于res/xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ListPreference
        android:title="@string/setting_sort_label"
        android:key="@string/setting_sort_key"
        android:entries="@array/movies_sort"
        android:entryValues="@array/movies_sort_values" />

</PreferenceScreen>

AndroidManifest.xml AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.brainchest.udacity.popularmovies">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SettingsActivity"
            android:label="@string/general_settings"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="net.brainchest.udacity.popularmovies.MainActivity"></meta-data>
        </activity>
    </application>

</manifest>

onOptionsItemSelected in my Fragment : Here the intent is created and startActivity is called. 我的Fragment中的onOptionsItemSelected :在此创建意图并调用startActivity The debugger runs through it, no exceptions. 调试器通过它运行,没有例外。

/**
 * {@link Fragment#onOptionsItemSelected(MenuItem)}
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // open the settings
    if (item.getItemId() == R.id.action_settings) {
        Intent settingsIntent = new Intent(getContext(), SettingsActivity.class);
        startActivity(settingsIntent);
    }

    return super.onOptionsItemSelected(item);
}

arrays.xml arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="movies_sort">
        <item>Most popular</item>
        <item>Highest rated</item>
    </string-array>
    <string-array name="movies_sort_values">
        <item>0</item>
        <item>1</item>
    </string-array>
</resources>

something else I need to show here? 我需要在这里显示其他内容吗?

What I tried so far 到目前为止我尝试过的

Searching the web I found some common mistakes that refer either to a missing AndroidManifest.xml entry or mixing up some resource IDs. 在网上搜索时,我发现了一些常见的错误,这些错误指的是缺少的AndroidManifest.xml条目或混淆了一些资源ID。 I double checked this to my best knowledge but still it doesn't work. 据我所知,我对此进行了仔细检查,但仍然无法正常工作。

What did I miss? 我错过了什么?

Try wrapping the ListPreference in a PreferenceCategory like this: 尝试将ListPreference包装在PreferenceCategory中,如下所示:

<PreferenceCategory
    android:key="<YOUR_KEY>"
    android:title="<YOUR_TITILE>" >

    <ListPreference
       android:title="@string/setting_sort_label"
       android:key="@string/setting_sort_key"
       android:entries="@array/movies_sort"
       android:entryValues="@array/movies_sort_values" />
</PreferenceCategory>

Finally found out what the issue is. 终于找出问题所在。 I used the wrong overload. 我使用了错误的重载。 Instead of 代替

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);

    addPreferencesFromResource(R.xml.pref_movies);
}

which is not called, I had to use 不被称为,我不得不使用

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.pref_movies);
}

Now the settings are shown. 现在显示设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM