简体   繁体   中英

Can't get value from SharedPreferences

Hey, can anyone help me here? I want to get the SharedPreferences from User.java but I always get the following error:

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) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Runt 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) at android.app.Activity.performCreate(Activity.java:8000) at android.app.Activity.performCreate(Activity.Z93F725 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) at 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: Sending signal. PID: 22063 SIG: 9

Here my User.java, where the error causes:

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

public int getGetrunkenFromPreference(String name)

You cannot call public functions in a class extended from an activity as that activity cannot be instantiated using the new operator.

the right way to do this is:

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

You can modify it like that and then where you call the constructor of this class pass the activity context like if u create object of User calss in MainActivity.java then use it like this

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

}

Add Context as parameter for the member function and call getSharedPreference with that passed context as below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM