0

我的 onCheckedChanged 监听器出现空指针异常。

这是我班的代码:

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>
4

1 回答 1

0

PreferenceActivity 不用于在其中定义视图。您可以查看这些链接。制作单独的 java 文件来扩充您的视图。检查这些示例:

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

不做就找不到你的setContentView(...)观点onCreate()

于 2013-06-03T07:54:02.047 回答