保存字符串(Cookie)到SharedPrefs导致NullPointerException [英] Save String (Cookie) to SharedPrefs caused NullPointerException

查看:451
本文介绍了保存字符串(Cookie)到SharedPrefs导致NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个类,帮助我处理认证(保存Cookie到SharedPrefs)。

I´ve made a class with helps me to handle the Authentication (save Cookie to 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);
        }
}

当我调用 .setToken(token) -method我的回应(RegisterActivity)我会得到一个NullPointerException:

When I call the Authentication.setToken(token)-method my response (RegisterActivity) I´ll get a NullPointerException:

java.lang .NullPointerException:尝试调用接口方法'android.content.SharedPreferences $ Editor android.content.SharedPreferences $ Editor.putString(java.lang.String,java.lang.String)'在空对象引用

你们有人能帮我解决这个问题吗?感谢您提前

Can someone of you help me to solve this prob? Thanks in advance

推荐答案

您没有在清单中注册您的申请。
或使用代码
的第一个创建对象身份验证首先在清单中注册

you are not register your application in manifiest that. or first createonject of Authentication with your code first register it in manifiest

更改 / strong>与

change your Authentication with

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>

这篇关于保存字符串(Cookie)到SharedPrefs导致NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆