2011-07-10 64 views
1

我知道這不能是我認爲這是困難的。我確信我擁有我可以設想實現我的onClickListener的所有內容。我錯過了什麼,爲什麼這個力量會關閉?我已經提供了堆棧來看看。任何幫助都會很棒。這被認爲是保存用戶設置的簡單活動。如果我註釋掉這些代碼,活動將顯示活動。setOnPreferenceClickListener幫助,Forclosing當活動啓動

Preference customPref = (Preference) findPreference("customPref"); 

customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { //this line forcecloses every time i run the activity. 
    @Override 
    public boolean onPreferenceClick(Preference preference) { 
     Toast.makeText(getBaseContext(), 
         "The custom preference has been clicked", 
         Toast.LENGTH_LONG) 
      .show(); 
     SharedPreferences customSharedPreference = getSharedPreferences(
       "myCustomSharedPrefs", Activity.MODE_PRIVATE); 
     SharedPreferences.Editor editor = customSharedPreference.edit(); 
     editor.putString("myCustomPref", "The preference has been clicked"); 
     editor.commit(); 
     return true; 
    } 
}); 

日誌:

07-10 16:36:47.137: WARN/System.err(1042): java.lang.NullPointerException 
07-10 16:36:47.147: WARN/System.err(1042):  at com.nyneaxis.android.mpg.Preferences.onCreate(Preferences.java:22) 
07-10 16:36:47.147: WARN/System.err(1042):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-10 16:36:47.157: WARN/System.err(1042):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 
07-10 16:36:47.157: WARN/System.err(1042):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 
07-10 16:36:47.157: WARN/System.err(1042):  at android.app.ActivityThread.access$2200(ActivityThread.java:119) 
07-10 16:36:47.157: WARN/System.err(1042):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 
07-10 16:36:47.167: WARN/System.err(1042):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-10 16:36:47.167: WARN/System.err(1042):  at android.os.Looper.loop(Looper.java:123) 
07-10 16:36:47.167: WARN/System.err(1042):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
07-10 16:36:47.167: WARN/System.err(1042):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-10 16:36:47.177: WARN/System.err(1042):  at java.lang.reflect.Method.invoke(Method.java:521) 
07-10 16:36:47.177: WARN/System.err(1042):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
07-10 16:36:47.177: WARN/System.err(1042):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
07-10 16:36:47.177: WARN/System.err(1042):  at dalvik.system.NativeStart.main(Native Method) 
+0

看起來像'findPreference(「customPref」)'返回'null'。你能檢查一下嗎? –

回答

4

它看起來像你的customPref變量爲空。確保您在偏好xml中使用了customPref的偏好設置(如果使用的話),或者您可能在那裏拼錯了密鑰。

+0

看到我知道這很簡單。 –