簡體   English   中英

無法從 SharedPreferences 中獲取價值

[英]Can't get value from SharedPreferences

嘿,有人可以幫我嗎? 我想從 User.java 獲取 SharedPreferences 但我總是收到以下錯誤:

2020-12-29 16:42:11.424 22063-22063/com.example.bauwagenapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.bauwagenapp, PID: 22063 java.lang.RuntimeException: Unable to start activity ComponentInfo{ com.example.bauwagenapp/com.example.bauwagenapp.ActivityUser}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at android. app.ActivityThread.performLaunchActivity(ActivityThread.java:3449) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.Z93F725A 07423FE1C889F448B33D21F46Z:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread .java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656)在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit$MethodAndArgs imeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Caused by : java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:122) at com.example.bauwagenapp.User.getGuthabenFromPreference(User.java:44) at com.example.bauwagenapp.User.(User.java:39) at com.example.bauwagenapp.ActivityUser.onCreate(ActivityUser.java:43)在 android.app.Activity.performCreate(Activity.java:8000) 在 android.app.Activity.performCreate(Activity.Z93F72 A07423FE1C889F448B33D21F46Z:7984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)在 android.app.ActivityThread$H.handleMessage(ActivityThread.Z93F725A07423FE1C889F448 B33D21F46Z:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java :947) 2020-12-29 16:42:11.515 22063-22063/com.example.bauwagenapp I/Process: 發送信號。 PID:22063 SIG:9

這是我的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;
    }
}

公共 int getGetrunkenFromPreference(字符串名稱)

您不能在從活動擴展的 class 中調用公共函數,因為無法使用new運算符實例化該活動。

正確的做法是:

 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;
  }
}

您可以像這樣修改它,然后在調用此 class 的構造函數的位置傳遞活動上下文,就像您在 MainActivity.java 中創建用戶類的 object 一樣,然后像這樣使用它

用戶 user = new 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;
}

}

添加 Context 作為成員 function 的參數,並使用傳遞的上下文調用 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM