當我將它放入手機進行調試時,此活動崩潰,我只是添加了用於存儲變量的部件。我做錯什麼了嗎?Android共享首選項崩潰
這是一個活動,如果它們設置的更早,它應該得到當前變量,用戶也可以設置它們。
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:致命異常:主 過程:COM。 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)」 上的空對象引用 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094) 在 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) 在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) 引起:java.lang.NullPointerException:嘗試調用虛擬 方法'android.content.SharedPreferences android.content .Context.getSharedPreferences(java.lang.String,int)' null對象引用 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) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 在android.app.ActivityThread.access $ 1100(ActivityThread.java:222) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1795) at android.os.Han droid.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。運行(ZygoteInit.java:1230) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
您能提供崩潰記錄嗎? –
他們會在哪裏?使用android studio。 –
如果您在連接的設備或仿真器上運行程序,則可以在logcat中看到日誌。在Android Studio底部欄中搜索「Android Monitor」。 –