Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. 01-22 11:45:32.722 13037-13037/com.example.android.sunshine.app E/AndroidRuntime: FATAL EXCEPTION: main
  2. Process: com.example.android.sunshine.app, PID: 13037
  3. 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
  4. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491)
  5. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2564)
  6. at android.app.ActivityThread.access$800(ActivityThread.java:170)
  7. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
  8. at android.os.Handler.dispatchMessage(Handler.java:111)
  9. at android.os.Looper.loop(Looper.java:194)
  10. at android.app.ActivityThread.main(ActivityThread.java:5576)
  11. at java.lang.reflect.Method.invoke(Native Method)
  12. at java.lang.reflect.Method.invoke(Method.java:372)
  13. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956)
  14. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
  15. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.preference.Preference.setOnPreferenceChangeListener(android.preference.Preference$OnPreferenceChangeListener)' on a null object reference
  16. at com.example.android.sunshine.app.SettingsActivity.bindPreferenceSummaryToValue(SettingsActivity.java:40)
  17. at com.example.android.sunshine.app.SettingsActivity.onCreate(SettingsActivity.java:30)
  18. at android.app.Activity.performCreate(Activity.java:6041)
  19. at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
  20. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
  21. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2564) 
  22. at android.app.ActivityThread.access$800(ActivityThread.java:170) 
  23. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441) 
  24. at android.os.Handler.dispatchMessage(Handler.java:111) 
  25. at android.os.Looper.loop(Looper.java:194) 
  26. at android.app.ActivityThread.main(ActivityThread.java:5576) 
  27. at java.lang.reflect.Method.invoke(Native Method) 
  28. at java.lang.reflect.Method.invoke(Method.java:372) 
  29. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:956) 
  30. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751) 
  31.  
  32. <string name="app_name">Sunshine</string>
  33. <string name="hello_world">Hello world!</string>
  34. <string name="action_settings">Settings</string>
  35. <string name="action_refresh" translatable="false">Refresh</string>
  36. <string name="title_activity_detail">Details</string>
  37. <string name="title_activity_settings">Settings</string>
  38.  
  39.  
  40. <string name="pref_location_label" >Location</string>
  41.  
  42. <string name="pref_location_key" translatable="false">location</string>
  43.  
  44. <string name="pref_location_default" translatable="false">94043</string>
  45.  
  46. import android.os.Bundle;
  47. import android.preference.ListPreference;
  48. import android.preference.Preference;
  49. import android.preference.PreferenceActivity;
  50. import android.preference.PreferenceManager;
  51.  
  52. public class SettingsActivity extends PreferenceActivity
  53. implements Preference.OnPreferenceChangeListener {
  54.  
  55. @Override
  56. public void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58.  
  59. addPreferencesFromResource(R.xml.pref_general);
  60.  
  61. bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
  62. }
  63.  
  64. private void bindPreferenceSummaryToValue(Preference preference) {
  65.  
  66. preference.setOnPreferenceChangeListener(this);
  67.  
  68. onPreferenceChange(preference,
  69. PreferenceManager
  70. .getDefaultSharedPreferences(preference.getContext())
  71. .getString(preference.getKey(), ""));
  72. }
  73.  
  74. @Override
  75. public boolean onPreferenceChange(Preference preference, Object value) {
  76. String stringValue = value.toString();
  77.  
  78. if (preference instanceof ListPreference) {
  79. // For list preferences, look up the correct display value in
  80. // the preference's 'entries' list (since they have separate labels/values).
  81. ListPreference listPreference = (ListPreference) preference;
  82. int prefIndex = listPreference.findIndexOfValue(stringValue);
  83. if (prefIndex >= 0) {
  84. preference.setSummary(listPreference.getEntries()[prefIndex]);
  85. }
  86. } else {
  87. // For other preferences, set the summary to the value's simple string representation.
  88. preference.setSummary(stringValue);
  89. }
  90. return true;
  91. }
  92.  
  93. }
  94.  
  95. <?xml version="1.0" encoding="utf-8"?>
  96. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
  97. android:layout_width="match_parent"
  98. android:layout_height="match_parent"
  99. >
  100.  
  101. <EditTextPreference
  102. android:key="pref_location_key"
  103. android:title="@string/pref_location_label"
  104. android:defaultValue="@string/pref_location_default"
  105. android:inputType="text"
  106. android:singleLine="true" />
  107. </PreferenceScreen>
  108.  
  109. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
  110. 'void android.preference.Preference.setOnPreferenceChangeListener(android.preference.Preference$OnPreferenceChangeListener)'
  111. on a null object reference
  112.  
  113. preference.setOnPreferenceChangeListener(this);
  114.  
  115. bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
  116.  
  117. bindPreferenceSummaryToValue(findPreference("pref_location_key"));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement