2013-06-03 2 views
0

내 onCheckedChanged 수신기에서 nullpointer 예외가 발생합니다. OnCheckedChanged의 nullpointer

내 클래스의 코드입니다 :

public class Instellingen extends PreferenceActivity implements OnCheckedChangeListener{ 

    public static final int mode= Activity.MODE_PRIVATE; 
    public static final String KEY_LOCKACTIONS = "lockactions"; 
    public static final String KEY_VERSIE = "Versie"; 
    public static final String KEY_CHAUFFEUR = "Chauffeur"; 
    public static final String KEY_LATITUDE = "Latitude"; 
    public static final String KEY_LONGITUDE = "Longitude"; 
    public static final String KEY_NEEDLOCUPDATE = "NeedLocUpdate"; 
    public static final String KEY_NEEDAPPUPDATE = "NeedAppUpdate"; 
    public static final String KEY_NEEDSYNC = "NeedSync"; 
    public static final String KEY_SYNCCONTENT = "NeedSyncContent"; 
    public static final String KEY_THREADCOUNT = "ThreadCount"; 
    public static final String KEY_FREESYNCTHREAD = "FreeSyncThread"; 
    public static final long version = 0; 

    static SharedPreferences mySharedPreferences; 
    CheckBox cbLogFile, cbRemainLogged; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 
     mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); 
     cbRemainLogged = (CheckBox)findViewById(R.id.cbRemainLoggedPreferences); 
     cbRemainLogged.setOnCheckedChangeListener(this); 
    } 

    public static SharedPreferences.Editor GetEditor(Context context) 
    { 
     mySharedPreferences = context.getSharedPreferences("KCStandaardSettings", mode); 
     return mySharedPreferences.edit(); 
    } 

    public static void SetBoolean(Context context, String Name, Boolean Value) 
    { 
     if (Name == KEY_LOCKACTIONS) { 
      TLogFile.appendLog("i", KEY_LOCKACTIONS, Value.toString()); 
     } 
     SharedPreferences.Editor editor = GetEditor(context); 
     editor.putBoolean(Name,Value); 
     editor.commit(); 
    } 

    public static Boolean GetBoolean(Context context, String Name) 
    { 
     mySharedPreferences = context.getSharedPreferences("KCStandaardSettings", mode); 
     return mySharedPreferences.getBoolean(Name, false); 
    } 

    public static void SetFloat(Context context, String Name, Float Value) 
    { 
     SharedPreferences.Editor editor = GetEditor(context); 
     editor.putFloat(Name,Value); 
     editor.commit(); 
    } 

    public static Float GetFloat(Context context, String Name) 
    { 
     mySharedPreferences = context.getSharedPreferences("KCStandaardSettings", mode); 
     return mySharedPreferences.getFloat(Name, 0); 
    } 

    public static void SetDouble(Context context, String Name, Double Value) 
    { 
     SharedPreferences.Editor editor = GetEditor(context); 
     Float f = new Float(Value); 
     editor.putFloat(Name,f); 
     editor.commit(); 
    } 

    public static Double GetDouble(Context context, String Name) 
    { 
     mySharedPreferences = context.getSharedPreferences("KCStandaardSettings", mode); 
     return (double) mySharedPreferences.getFloat(Name, 0); 
    } 

    public static void SetLong(Context context, String Name, Long Value) 
    { 
     SharedPreferences.Editor editor = GetEditor(context); 
     editor.putLong(Name,Value); 
     editor.commit(); 
    } 

    public static Long GetLong(Context context, String Name) 
    { 
     mySharedPreferences = context.getSharedPreferences("KCStandaardSettings", mode); 
     return mySharedPreferences.getLong(Name, 0); 
    } 

    public static void SetString(Context context, String Name, String Value) 
    { 
     SharedPreferences.Editor editor = GetEditor(context); 
     editor.putString(Name,Value); 
     editor.commit(); 
    } 

    public static String GetString(Context context, String Name) 
    { 
     mySharedPreferences = context.getSharedPreferences("KCStandaardSettings", mode); 
     return mySharedPreferences.getString(Name, ""); 
    } 

    public static void SetInt(Context context, String Name, int Value) 
    { 
     SharedPreferences.Editor editor = GetEditor(context); 
     editor.putInt(Name,Value); 
     editor.commit(); 
    } 

    public static int GetInt(Context context, String Name) 
    { 
     mySharedPreferences = context.getSharedPreferences("KCStandaardSettings", mode); 
     return mySharedPreferences.getInt(Name, 0); 
    } 

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     switch(buttonView.getId()){ 
     case R.id.cbRemainLoggedPreferences: 
      Instellingen.SetBoolean(this, "RemainLoggedIn", isChecked); 
      break; 
     } 
    } 
    } 

이 오류 로그입니다 :

여기
06-03 09:18:03.603: E/AndroidRuntime(14208): FATAL EXCEPTION: main 
06-03 09:18:03.603: E/AndroidRuntime(14208): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.koeriers.standaard/com.koeriers.standaard.Instellingen}: java.lang.NullPointerException 
06-03 09:18:03.603: E/AndroidRuntime(14208): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2246) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at android.app.ActivityThread.access$700(ActivityThread.java:151) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at android.os.Handler.dispatchMessage(Handler.java:99) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at android.os.Looper.loop(Looper.java:137) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at android.app.ActivityThread.main(ActivityThread.java:5293) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at java.lang.reflect.Method.invokeNative(Native Method) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at java.lang.reflect.Method.invoke(Method.java:511) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at dalvik.system.NativeStart.main(Native Method) 
06-03 09:18:03.603: E/AndroidRuntime(14208): Caused by: java.lang.NullPointerException 
06-03 09:18:03.603: E/AndroidRuntime(14208): at com.koeriers.standaard.Instellingen.onCreate(Instellingen.java:38) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at android.app.Activity.performCreate(Activity.java:5250) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 
06-03 09:18:03.603: E/AndroidRuntime(14208): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210) 
06-03 09:18:03.603: E/AndroidRuntime(14208): ... 11 more 

내 클래스의 XML입니다. 이것은 addPreferencesFromResource()에서 호출됩니다.

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

<EditTextPreference 
    android:id="@+id/etLicensePreferences" 
    android:key="LicentieCode" 
    android:title="LicentieCode: " /> 
<CheckBoxPreference 
    android:id="@+id/cbRemainLoggedPreferences" 
    android:defaultValue="false" 
    android:key="cbLoggedOn" 
    android:title="@string/remainLoggedIn" /> 
<CheckBoxPreference 
    android:id="@+id/cbNotificationSound" 
    android:defaultValue="false" 
    android:key="cbNotificationSound" 
    android:title="@string/Sound" /> 

<CheckBoxPreference 
    android:id="@+id/cbWidgetAllPreferences" 
    android:defaultValue="true" 
    android:key="cbWidgetAll" 
    android:title="Toon in de Widget de tab met alle berichten" /> 
<CheckBoxPreference 
    android:id="@+id/cbWidgetUnreadPreferences" 
    android:defaultValue="true" 
    android:key="cbWidgetUnread" 
    android:title="Toon in de Widget de tab met ongelezen berichten" /> 
<CheckBoxPreference 
    android:id="@+id/cbWidgetReadPreferences" 
    android:defaultValue="true" 
    android:key="cbWidgetRead" 
    android:title="Toon in de Widget de tab met gelezen berichten" /> 

</PreferenceScreen> 
+0

당신의 줄 번호 38 인 onCreate()setContentView(...)를하지 않고 귀하의 의견을 찾을 수 있습니까? –

+0

addPreferencesFromResource (R.xml.preferences);는 무엇입니까? setContentView()는 어디에 있습니까? – Sam

+1

addPreferencesFromResource는 PreferenceActivity이고 일반 Activity가 아니므로 호출합니다. 따라서이 경우 setContentView는 작동하지 않습니다. – user

답변

0

PreferenceActivity는 내부에서보기를 정의하는 데 사용되지 않습니다. 이러한 링크를 확인할 수 있습니다. 당신의 전망을 팽창시키는 별도의 자바 파일을 만드십시오. 이 예제를 확인하십시오

http://viralpatel.net/blogs/android-preferences-activity-example/

http://androidresearch.wordpress.com/2012/03/09/creating-a-preference-activity-in-android/

http://marakana.com/bookshelf/main_building_blocks_tutorial/preference_activity.html

당신은

+0

아우, 감사합니다. 정말 바보 같았습니다. 필자는 각 Preference 객체에 정의 된 키를 XML 파일에서 사용해야한다는 것을 알고 있어야합니다. 어쨌든 고마워. – user

+0

당신은 환영합니다, 친구 ... –