我正在通过Udacity Android Development课程,当我点击设置时,应用程序崩溃了。我遵循这里给出的代码:(https://github.com/udacity/SunshineVersion2/blob/3.13_add_share_action_provider/app/src/main/java/com/example/android/sunshine/app/SettingsActivity.java)。
我在这里也找到了类似的帖子:(PreferenceFragment NullPointerException)。但他们都没有真正帮助过。我知道Preference Activity中的方法已被弃用,我试图将API作为目标并将其转换为片段,但这两个选项都不起作用。单击设置按钮时,下面是logcat消息:
AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.sunshine.app, PID: 24808
java.lang.RuntimeException: Unable to start activity
java.lang.NullPointerException: Attempt to invoke virtualmethod 'void android.preference.Preference.setOnPreferenceChangeListener(android.preference.Preference$OnPreferenceChangeListener)' on a null object reference
以下是设置活动的代码:
public class SettingsActivity extends PreferenceActivity
implements Preference.OnPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add 'general' preferences, defined in the XML file
addPreferencesFromResource(R.xml.pref_general);
// For all preferences, attach an OnPreferenceChangeListener so the UI summary can be
// updated when the preference changes.
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_units_key)));
}
/**
* Attaches a listener so the summary is always updated with the preference value.
* Also fires the listener once, to initialize the summary (so it shows up before the value
* is changed.)
*/
private void bindPreferenceSummaryToValue(Preference preference) {
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(this);
// Trigger the listener immediately with the preference's
// current value.
onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list (since they have separate labels/values).
ListPreference listPreference = (ListPreference) preference;
int prefIndex = listPreference.findIndexOfValue(stringValue);
if (prefIndex >= 0) {
preference.setSummary(listPreference.getEntries()[prefIndex]);
}
} else {
// For other preferences, set the summary to the value's simple string representation.
preference.setSummary(stringValue);
}
return true;
}
}
下面是我的strings.xml:
<resources>
<string name="app_name">Sunshine</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="action_refresh">Refresh</string>
<string name="title_activity_detail">DetailActivity</string>
<string name="title_activity_settings">SettingsActivity</string>
<string name = "pref_location_key">location</string>
<string name="pref_location_label">Location</string>
<string name="pref_location_default">82072</string>
<!-- Label for the temperature units preference [CHAR LIMIT=30] -->
<string name="pref_units_label">Temperature Units</string>
<!-- Label for metric option in temperature unit preference [CHAR LIMIT=25] -->
<string name="pref_units_label_metric">Metric</string>
<!-- Label for imperial option in temperature unit preference [CHAR LIMIT=25] -->
<string name="pref_units_label_imperial">Imperial</string>
<!-- Key name for temperature unit preference in SharedPreferences [CHAR LIMIT=NONE] -->
<string name="pref_units_key" translatable="false">units</string>
<!-- Value in SharedPreferences for metric temperature unit option [CHAR LIMIT=NONE] -->
<string name="pref_units_metric" translatable="false">metric</string>
<!-- Value in SharedPreferences for imperial temperature unit option [CHAR LIMIT=NONE] -->
<string name="pref_units_imperial" translatable="false">imperial</string>
这是我的pref_general.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditTextPreference
android:key = "Location"
android:title = "Location"
android:defaultValue= "82072"
android:inputType = "text"
android:singleLine="true"
/>
<ListPreference
android:title= "@string/pref_units_label"
android:key = "@string/pref_units_key"
android:defaultValue="@string/pref_units_metric"
android:entryValues="@array/pref_units_values"
android:entries="@array/pref_units_options"
/>
</PreferenceScreen>
感谢您提前抽出时间和帮助。
答案 0 :(得分:1)
大多数编程语言和操作系统都区分大小写。 android:key = "Location"
与<string name = "pref_location_key">location</string>
不符。
答案 1 :(得分:0)
对我来说,我正在使用
bindPreferenceSummaryToValue(findPreference("example_list"));
而不是
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_temp_units_key)));