调用 sharedpreferences 时应用程序崩溃

App crashes when sharedpreferencs called

我正在使用 android studio,但我 运行 遇到了问题。 我正在尝试调用具有共享首选项的首选项,但是当我到达该行时,应用程序崩溃了。 如果我正在正确阅读 logcat,因为共享首选项为空。

public class counter extends AppCompatActivity {
    int timer1 = 0;
    String sec= "0";
    CountDownTimer cTimer = null;
    SharedPreferences mPrefs = getSharedPreferences("cdtime", 0);
    int cdtime = mPrefs.getInt("cdtime", 300000);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_counter);

        cTimer = new CountDownTimer(cdtime, 1000){

            @Override
            public void onTick(long millisUntilFinished) {
                TextView v = (TextView) findViewById(R.id.textView);
                long min = ((millisUntilFinished/1000)/60);
                long secprime = ((millisUntilFinished-(min*60000))/1000);
                if(secprime>10){
                    sec = (""+secprime);
                }else {
                    sec = ("0" + secprime);
                }
            }
        }
    }
}

我已经包含了 logcat。 我试图通过添加

来解决问题
if(cdtime=null){
    //action
}

但这会引发语法错误。我究竟做错了什么? 注意:我也尝试将代码移动到 .super 之后的 on.Create 之后 它抛出同样的错误 值 cdtime 用于设置计时器的时间,我试图将此设置在单独的 activity 中用于 'settings'。

logcat

32014-32014/com.example.user.medialert E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.user.medialert, PID: 32014 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.user.medialert/com.example.user.medialert.counter}: 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:2515) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.access0(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5832) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 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 android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:184) at com.example.user.medialert.counter.(counter.java:26) at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.Class.newInstance(Class.java:1650) at android.app.Instrumentation.newActivity(Instrumentation.java:1079) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2505) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) at android.app.ActivityThread.access0(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5832) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 09-25 18:29:14.221 647-647/com.example.user.medialert E/Zygote: MountEmulatedStorage() 09-25 18:29:14.221 647-647/com.example.user.medialert E/Zygote: v2 09-25 18:29:14.231 647-647/com.example.user.medialert E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL

可能的解决方案如下:

//改变这个

SharedPreferences mPrefs = getSharedPreferences("cdtime", 0);

//用这个

SharedPreferences mPrefs = this.getPreferences(Context.MODE_PRIVATE);

如果它对您有任何改变,请告诉我。

编辑:更新后你的新代码应该看起来像这样 -

public class counter extends AppCompatActivity {
    int timer1 = 0;
    String sec= "0";
    CountDownTimer cTimer = null;
    SharedPreferences mPrefs;
    int cdtime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_counter);

        mPrefs = this.getPreferences(Context.MODE_PRIVATE);
        cdtime = mPrefs.getInt("cdtime", 300000);



        cTimer = new CountDownTimer(cdtime, 1000){

            @Override
            public void onTick(long millisUntilFinished) {
                TextView v = (TextView) findViewById(R.id.textView);
                long min = ((millisUntilFinished/1000)/60);
                long secprime = ((millisUntilFinished-(min*60000))/1000);
                if(secprime>10){
                    sec = (""+secprime);
                }else {
                    sec = ("0" + secprime);
                }
            }
        }
    }
}