Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

java - 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.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.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(RuntimeInit.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.java: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.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(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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...