【问题标题】:Save String (Cookie) to SharedPrefs caused NullPointerException将字符串(Cookie)保存到 SharedPrefs 导致 NullPointerException
【发布时间】:2016-06-20 22:03:40
【问题描述】:

我创建了一个类来帮助我处理身份验证(将 Cookie 保存到 SharedPrefs)。

public class Authentication extends Application {

    String PREFS_NAME = "UserData";
    String DEFAULT = "";

    Context context;
    public static SharedPreferences sharedPreferences;
    public static SharedPreferences.Editor editor;
    public static String token;

    public Authentication(Activity context) {
        this.context = context;
        sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        token = sharedPreferences.getString("Cookie", DEFAULT);
    }

    //speichert Token in den Shared Preferences
    public static void setToken(String token) {
        Log.d("Cookie", token);
        editor.putString("Cookie", token);
        }
}

当我调用Authentication.setToken(token)-方法时,我的响应(RegisterActivity)我会得到一个 NullPointerException:

java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference

有人可以帮我解决这个问题吗?提前致谢

【问题讨论】:

  • 从方法setToken中删除静态关键字并尝试
  • 我猜你在调用setToken()之前没有调用Authentication()...所以你的editor没有被初始化
  • @SagarThakarar 去除静电并不能解决问题...
  • @Yvette String token 来自我在 RegisterActivity 中的 HTTP-Response!

标签: java android cookies nullpointerexception android-sharedpreferences


【解决方案1】:

你没有在manifest中注册你的应用程序。 或首先使用您的代码创建 Authentication 首先在manifest中注册它

使用

更改您的身份验证
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class Authentication extends Application {
    String PREFS_NAME = "UserData";
    String DEFAULT = "";
    Context context;
    public static SharedPreferences sharedPreferences;
    public static SharedPreferences.Editor editor;
    public static String token;

    public Authentication() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context  = this;
        sharedPreferences = getSharedPreferences(PREFS_NAME,
                Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        token = sharedPreferences.getString("Cookie", DEFAULT);
    }

    // speichert Token in den Shared Preferences
    public static void setToken(String token) {
        Log.d("Cookie", token);
        if(editor==null){
            throw new NullPointerException("Register your application "+Authentication.class+" in AndroidManifiest.xml");
        }
        editor.putString("Cookie", token);
    }

}

AndroidManifiest.xml

<application
        android:name="com.android.Authentication"
        android:icon="@mipmap/ic_launcher_home"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.blue"
         >
.
.
.
 <activity.../>
<service.../>

  </application>

【讨论】:

  • 好的...这种方式不会引发 NullPointerException,但我的令牌没有保存在 SharedPrefs 中...有什么建议吗?
  • 啊...没关系!你忘了打电话给editor.apply()!现在完美运行,谢谢:)
猜你喜欢
  • 1970-01-01
  • 2011-05-23
  • 2016-09-11
  • 2011-03-05
  • 2016-10-22
  • 1970-01-01
  • 2012-02-03
  • 2015-02-07
  • 2013-03-27
相关资源
最近更新 更多