class 中的空共享首选项

null shared preferences in a class

对于一些常见的活动流程,我创建了一个 class CurrentDate,它获取实际日期,return de date 在不同的本地偏好顺序中欧洲(dd/mm/yy),美国(mm/dd/yy...我创建了一些首选项数据。int,字符串...问题是在我的 class 中加载首选项数据,没有任何 onCreate....因为不是activity,因此它没有加载共享首选项的实例。并给出一个空错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
    at com.example.ricmac.task2.CurrentDate.<init>(CurrentDate.java:50)

此 class,CurrentDate 是从 activity:`

调用的
public class CurrentDate {

private static final int PREFS_MODE = 0;
private static final String PREFS_FILE = "pref";


public CurrentDate (Context context){ mContext = context;}

< 这里是第 50 行的失败:

final SharedPreferences settings = mContext.getSharedPreferences(PREFS_FILE, PREFS_MODE);

< 继续 CurrentDate 的代码 class

public void checkDate(TextView tstart){
    String iCurrent = mContext.getResources().getString(R.string.actual); // "current"
    String START = tstart.getText().toString(); // get string start date
    int localDatePreferencePosition = settings.getInt(KEY_LOCAL_DATE, 0); // get from shared preferences the preferences 0  European, 1 American,....
    if(START.equalsIgnoreCase(iCurrent)){ // check if it the current default date
        
        tstart.setText(getSetLocalDate(getCurrent(), localDatePreferencePosition,mContext)); // set the date in local format in textView tstar
    }

}

`

complete Error from Logical:

`

2020-10-18 12:18:52.383 25809-25809/com.example.ricmac.task2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ricmac.task2, PID: 25809
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ricmac.task2/com.example.ricmac.task2.introTaskFullScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
    at com.example.ricmac.task2.CurrentDate.<init>(CurrentDate.java:50)
    at com.example.ricmac.task2.introTaskFullScreen.onCreate(introTaskFullScreen.java:184)
    at android.app.Activity.performCreate(Activity.java:7009)
    at android.app.Activity.performCreate(Activity.java:7000)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6494) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

`

你的问题是 mContext 为空。

与其在上下文为 null 时在第 50 行创建设置对象,不如在 checkDate 方法本身中获取共享首选项,使用 TextView 获取上下文(我认为 tStart.getContext() 应该有效)