SharedPrefs空指针| 这不可能吗?

EGH*_*HDK 0 java android sharedpreferences

我在共享的pref上得到一个空指针.但共享首选项始终需要默认值.所以这不会让它变得不可能.

添加此代码段(粗略)给我的问题(根据logcat):

myPrefs = getSharedPreferences("myPrefs", 0);
        String lvl_result= myPrefs
                .getString(lvl, null);
        // Example "A -> a" or null
        if (lvl_result.equals(null)) {
            gen_lvl();
            lvl_result = myPrefs.getString(lvl, null);
        }
Run Code Online (Sandbox Code Playgroud)

Logcat(不确定这是否是您想要看到的):

12-04 00:54:42.932: E/AndroidRuntime(763): Caused by: java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)

Pra*_*gar 5

替换代码中的以下行:

if (lvl_result.equals(null)) {
Run Code Online (Sandbox Code Playgroud)

if (lvl_result == null) {
Run Code Online (Sandbox Code Playgroud)

如果lvl_resultnull然后调用equals此对象的方法将引发空指针异常..