当我将它放入手机进行调试时,此活动崩溃,我只是添加了存储变量的部分。我做错了吗?
这是一个活动,如果他们之前设置了它们,它们应该获取当前变量,用户也可以设置它们。
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();
}
}
}
更新崩溃日志:
03-18 15:29:59.477 7801-7801 / com.software.roux.diabcalc E / AndroidRuntime:致命异常:主要 处理:com.software.roux.diabcalc,PID:7801 java.lang.RuntimeException:无法实例化活动 ComponentInfo {com.software.roux.diabcalc / com.software.roux.diabcalc.Settings}: java.lang.NullPointerException:尝试调用虚方法 “android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String,int)' 在null对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 在android.app.ActivityThread.access $ 1100(ActivityThread.java:222) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1795) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:158) 在android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 引起:java.lang.NullPointerException:尝试调用虚拟 方法'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String,int)' 在null对象引用上 在 android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:185) 在com.software.roux.diabcalc.Settings。(Settings.java:21) at java.lang.Class.newInstance(Native Method) 在android.app.Instrumentation.newActivity(Instrumentation.java:1095) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3084) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 在android.app.ActivityThread.access $ 1100(ActivityThread.java:222) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1795) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:158) 在android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:1)
稍后启动SharedPreferences mPrefs
。在onCreate
方法中。
仅在context
方法之后初始化onCreate
。
所以,这必须工作:
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);
另一个问题是,在当前代码中,您只需将EditText
变量的地址保存在内存中。要检索文本,请使用:
String bolussave = ""+b1.getText().toString();
答案 1 :(得分:0)
尝试在OnCreate方法中实例化mPref。
@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);
...
...