gpt4 book ai didi

java - Udacity Android 开发设置导致致命异常

转载 作者:行者123 更新时间:2023-11-30 01:39:24 24 4
gpt4 key购买 nike

我正在学习 Udacity Android 开发类(class),当我点击设置时,应用程序崩溃了。我遵循此处给出的代码:( 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 为目标并将其转换为 fragment ,但这两个选项都不起作用。以下是我单击设置按钮时的 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

下面是设置 Activity 的代码:

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>

提前感谢您抽出宝贵时间提供帮助。

最佳答案

大多数编程语言和操作系统都区分大小写。 android:key = "Location"不匹配 <string name = "pref_location_key">location</string> .

关于java - Udacity Android 开发设置导致致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34698380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com