Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 01-22 11:45:32.722 13037-13037/com.example.android.sunshine.app E/AndroidRuntime: FATAL EXCEPTION: main
- Process: com.example.android.sunshine.app, PID: 13037
- java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.sunshine.app/com.example.android.sunshine.app.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.Preference.setOnPreferenceChangeListener(android.preference.Preference$OnPreferenceChangeListener)' on a null object reference
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2564)
- at android.app.ActivityThread.access$800(ActivityThread.java:170)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
- at android.os.Handler.dispatchMessage(Handler.java:111)
- at android.os.Looper.loop(Looper.java:194)
- at android.app.ActivityThread.main(ActivityThread.java:5576)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.Preference.setOnPreferenceChangeListener(android.preference.Preference$OnPreferenceChangeListener)' on a null object reference
- at com.example.android.sunshine.app.SettingsActivity.bindPreferenceSummaryToValue(SettingsActivity.java:40)
- at com.example.android.sunshine.app.SettingsActivity.onCreate(SettingsActivity.java:30)
- at android.app.Activity.performCreate(Activity.java:6041)
- at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2564)
- at android.app.ActivityThread.access$800(ActivityThread.java:170)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
- at android.os.Handler.dispatchMessage(Handler.java:111)
- at android.os.Looper.loop(Looper.java:194)
- at android.app.ActivityThread.main(ActivityThread.java:5576)
- at java.lang.reflect.Method.invoke(Native Method)
- at java.lang.reflect.Method.invoke(Method.java:372)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
- <string name="app_name">Sunshine</string>
- <string name="hello_world">Hello world!</string>
- <string name="action_settings">Settings</string>
- <string name="action_refresh" translatable="false">Refresh</string>
- <string name="title_activity_detail">Details</string>
- <string name="title_activity_settings">Settings</string>
- <string name="pref_location_label" >Location</string>
- <string name="pref_location_key" translatable="false">location</string>
- <string name="pref_location_default" translatable="false">94043</string>
- import android.os.Bundle;
- import android.preference.ListPreference;
- import android.preference.Preference;
- import android.preference.PreferenceActivity;
- import android.preference.PreferenceManager;
- public class SettingsActivity extends PreferenceActivity
- implements Preference.OnPreferenceChangeListener {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- addPreferencesFromResource(R.xml.pref_general);
- bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
- }
- private void bindPreferenceSummaryToValue(Preference preference) {
- preference.setOnPreferenceChangeListener(this);
- 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;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- >
- <EditTextPreference
- android:key="pref_location_key"
- android:title="@string/pref_location_label"
- android:defaultValue="@string/pref_location_default"
- android:inputType="text"
- android:singleLine="true" />
- </PreferenceScreen>
- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
- 'void android.preference.Preference.setOnPreferenceChangeListener(android.preference.Preference$OnPreferenceChangeListener)'
- on a null object reference
- preference.setOnPreferenceChangeListener(this);
- bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
- bindPreferenceSummaryToValue(findPreference("pref_location_key"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement