简体   繁体   中英

Android Shared Preferences Crashes

This activity crashes when I put it in my phone to debug, I just added the parts for storing variables. Did I do something wrong?

It's an activity and is supposed to get the current variables if they set them earlier, and the user can also set them.

package com.software.roux.diabcalc;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;

import static com.software.roux.diabcalc.R.id.bolus;

public class Settings extends AppCompatActivity {

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

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        setTitle("Insulin Calculator by Ben Roux");

        String bolusString = mPrefs.getString("bolus", "0");
        String corrString = mPrefs.getString("correction", "0");
        String lowString = mPrefs.getString("low", "0");
        String highString = mPrefs.getString("high", "0");

        EditText b1 = (EditText)findViewById(R.id.bolus);
        b1.setText(bolusString);

        EditText b2 = (EditText)findViewById(R.id.correction);
        b2.setText(corrString);

        EditText b3 = (EditText)findViewById(R.id.targetlow);
        b3.setText(lowString);

        EditText b4 = (EditText)findViewById(R.id.targethigh);
        b4.setText(highString);
    }

    public void switchclick(View a) {
        if (a.getId() == R.id.backbutton) {
            Intent myIntent = new Intent(Settings.this, MainActivity.class);
            Settings.this.startActivity(myIntent);
        }
    }

    public void setclick(View b) {
        if (b.getId() == R.id.setter) {

            EditText b1 = (EditText)findViewById(R.id.bolus);
            String bolussave = ""+b1;
            SharedPreferences.Editor mEditor1 = mPrefs.edit();
            mEditor1.putString("bolus", bolussave).commit();

            EditText b2 = (EditText)findViewById(R.id.correction);
            String corrsave = ""+b2;
            SharedPreferences.Editor mEditor2 = mPrefs.edit();
            mEditor2.putString("correction", corrsave).commit();


            EditText b3 = (EditText)findViewById(R.id.targetlow);
            String lowsave = ""+b3;
            SharedPreferences.Editor mEditor3 = mPrefs.edit();
            mEditor3.putString("low", lowsave).commit();


            EditText b4 = (EditText)findViewById(R.id.targethigh);
            String highsave = ""+b4;
            SharedPreferences.Editor mEditor4 = mPrefs.edit();
            mEditor4.putString("high", highsave).commit();


        }
    }
}

UPDATES CRASH LOG:

03-18 15:29:59.477 7801-7801/com.software.roux.diabcalc E/AndroidRuntime: FATAL EXCEPTION: main Process: com.software.roux.diabcalc, PID: 7801 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.software.roux.diabcalc/com.software.roux.diabcalc.Settings}: 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:3094) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) at android.app.ActivityThread.access$1100(ActivityThread.java:222) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) at com.android.inte rnal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 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:185) at com.software.roux.diabcalc.Settings.(Settings.java:21) at java.lang.Class.newInstance(Native Method) at android.app.Instrumentation.newActivity(Instrumentation.java:1095) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3084) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) at android.app.ActivityThread.access$1100(ActivityThread.java:222) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android. app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Just initate SharedPreferences mPrefs later. In onCreate method.

the context is initialized only after onCreate method. So, this have to work:

SharedPreferences mPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        setTitle("Insulin Calculator by Ben Roux");
 mPrefs= getSharedPreferences("label", 0);

As the other question, in your current code you just saves address of EditText variable in the memory. To retrive the text, use this:

String bolussave = ""+b1.getText().toString();

Try instantiating your mPrefs inside your OnCreate method.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    setTitle("Insulin Calculator by Ben Roux");

    String bolusString = mPrefs.getString("bolus", "0");
    mPrefs = getSharedPreferences("label", 0);
    ...
    ...

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