无法从SharedReferences获取值

n9vozmp4  于 2021-06-27  发布在  Java
关注(0)|答案(4)|浏览(326)

嘿,有人能帮我吗?我想从user.java获取SharedReference,但总是出现以下错误:
2020-12-29 16:42:11.424 22063-22063/com.example.bauvanapp e/androidruntime:致命异常:主进程:com.example.bauvanapp,pid:22063 java.lang.runtimeexception:无法启动activity componentinfo{com.example.bauvanapp/com.example.bauvanapp.activityuser}:java.lang.nullpointerexception:尝试对位于的空对象引用调用虚拟方法“android.content.context android.content.context.getapplicationcontext()”android.app.activitythread.performlaunchactivity(活动线程。java:3449)在android.app.activitythread.handlelaunchactivity(activitythread。java:3601)在android.app.servertransaction.launchactivityitem.execute(launchactivityitem。java:85)在android.app.servertransaction.transactionexecutor.executecallbacks(transactionexecutor。java:135)在android.app.servertransaction.transactionexecutor.execute(transactionexecutor。java:95)在android.app.activitythread$h.handlemessage上。java:2066)在android.os.handler.dispatchmessage(handler。java:106)在android.os.looper.loop(looper。java:223)在android.app.activitythread.main(activitythread。java:7656)在java.lang.reflect.method.invoke(本机方法)位于com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit)。java:592)在com.android.internal.os.zygoteinit.main(zygoteinit。java:947)
原因:java.lang.nullpointerexception:尝试在android.content.contextwrapper.getapplicationcontext(contextwrapper)的空对象引用上调用虚拟方法“android.content.context android.content.context.getapplicationcontext()”。java:122)在com.example.bauvanapp.user.getguthabenfromfrompreference(user。java:44)在com.example.bauvanapp.user.(用户。java:39)在com.example.bauvagenapp.activityuser.oncreate(activityuser。java:43)在android.app.activity.performcreate(activity。java:8000)在android.app.activity.performcreate(activity。java:7984)在android.app.instrumentation.callactivityoncreate(instrumentation。java:1309)在android.app.activitythread.performlaunchactivity(活动线程。java:3422)在android.app.activitythread.handlelaunchactivity(activitythread。java:3601)在android.app.servertransaction.launchactivityitem.execute(launchactivityitem。java:85)在android.app.servertransaction.transactionexecutor.executecallbacks(transactionexecutor。java:135)在android.app.servertransaction.transactionexecutor.execute(transactionexecutor。java:95)在android.app.activitythread$h.handlemessage上。java:2066)在android.os.handler.dispatchmessage(handler。java:106)在android.os.looper.loop(looper。java:223)在android.app.activitythread.main(activitythread。java:7656)在java.lang.reflect.method.invoke(本机方法)位于com.android.internal.os.runtimeinit$methodandargscaller.run(runtimeinit)。java:592)在com.android.internal.os.zygoteinit.main(zygoteinit。java:947)2020-12-29 16:42:11.515 22063-22063/com.example.bauvanapp i/进程:发送信号。pid:22063信号:9
这里是my user.java,错误导致:

package com.example.bauwagenapp;

import android.content.SharedPreferences;
import androidx.appcompat.app.AppCompatActivity;

public class User extends AppCompatActivity {

    private String name;
    private int guthaben;
    private int getrunken;

    public void setName(String name){
        this.name = name;
    }

    public String getName(){
        return name;
    }

    public void setGuthaben(int guthaben){
        this.guthaben = guthaben;
    }

    public int getGuthaben(){
        return guthaben;
    }

    public void setGetrunken(int getrunken){
        this.getrunken = getrunken;
    }

    public int getGetrunken(){
        return getrunken;
    }

    public User(String name){
        this.name = name;
        this.guthaben = getGuthabenFromPreference(this.name);
        this.getrunken = getGetrunkenFromPreference(this.name);
    }

    public int getGuthabenFromPreference(String name){
        SharedPreferences Guthaben_User = getApplicationContext().getSharedPreferences(name, 0);
        int Guthaben = Guthaben_User.getInt("Guthaben", 0);
        return Guthaben;
    }

    public int getGetrunkenFromPreference(String name){
        SharedPreferences Getrunken_User = getApplicationContext().getSharedPreferences(name, 0);
        int Getrunken = Getrunken_User.getInt("Getrunken", 0);
        return Getrunken;
    }
}
ycggw6v2

ycggw6v21#

正确的方法是:

public class MyActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       User user = new User(getApplicationContext(),"NAME");
   }
}

public class User {

private String name;
private int guthaben;
private int getrunken;

public void setName(String name){
    this.name = name;
}

public String getName(){
    return name;
}

public void setGuthaben(int guthaben){
    this.guthaben = guthaben;
}

public int getGuthaben(){
    return guthaben;
}

public void setGetrunken(int getrunken){
    this.getrunken = getrunken;
}

public int getGetrunken(){
    return getrunken;
}

public User(Context context, String name){
    this.name = name;
    this.guthaben = getGuthabenFromPreference(context, this.name);
    this.getrunken = getGetrunkenFromPreference(context, this.name);
}

public int getGuthabenFromPreference(Context context, String name){
    SharedPreferences Guthaben_User = context.getSharedPreferences(name, 0);
    int Guthaben = Guthaben_User.getInt("Guthaben", 0);
    return Guthaben;
}

public int getGetrunkenFromPreference(Context context,String name){
    SharedPreferences Getrunken_User = context.getSharedPreferences(name, 0);
    int Getrunken = Getrunken_User.getInt("Getrunken", 0);
    return Getrunken;
  }
}
a7qyws3x

a7qyws3x2#

public int getrunkenfromPreference(字符串名称)
不能在从活动扩展的类中调用公共函数,因为不能使用 new 接线员。

kuhbmx9i

kuhbmx9i3#

您可以这样修改它,然后在调用这个类的构造函数的地方传递活动上下文,就像您在mainactivity.java中创建user calss的对象一样,然后像这样使用它
user user=新用户(“hello”,mainactivity.this);

public class User extends AppCompatActivity {

private String name;
private int guthaben;
private int getrunken;
private Context context;

public void setName(String name){
    this.name = name;
}

public String getName(){
    return name;
}

public void setGuthaben(int guthaben){
    this.guthaben = guthaben;
}

public int getGuthaben(){
    return guthaben;
}

public void setGetrunken(int getrunken){
    this.getrunken = getrunken;
}

public int getGetrunken(){
    return getrunken;
}

public User(String name, Context context){
    this.name = name;
    this.context = context;
    this.guthaben = getGuthabenFromPreference(this.name);
    this.getrunken = getGetrunkenFromPreference(this.name);
}

public int getGuthabenFromPreference(String name){
    SharedPreferences Guthaben_User = context.getSharedPreferences(name, 0);
    int Guthaben = Guthaben_User.getInt("Guthaben", 0);
    return Guthaben;
}

public int getGetrunkenFromPreference(String name){
    SharedPreferences Getrunken_User = context.getSharedPreferences(name, 0);
    int Getrunken = Getrunken_User.getInt("Getrunken", 0);
    return Getrunken;
}

}

vddsk6oq

vddsk6oq4#

添加context作为成员函数的参数,并使用传递的context调用getsharedpreference,如下所示

public int getGuthabenFromPreference(Context context, String name){
    SharedPreferences Guthaben_User = context.getSharedPreferences(name, 0);
    int Guthaben = Guthaben_User.getInt("Guthaben", 0);
    return Guthaben;
}

public int getGetrunkenFromPreference(Context context,String name){
    SharedPreferences Getrunken_User = context.getSharedPreferences(name, 0);
    int Getrunken = Getrunken_User.getInt("Getrunken", 0);
    return Getrunken;
}

相关问题