2017-03-18 198 views
1

當我將它放入手機進行調試時,此活動崩潰,我只是添加了用於存儲變量的部件。我做錯什麼了嗎?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)

+0

您能提供崩潰記錄嗎? –

+0

他們會在哪裏?使用android studio。 –

+0

如果您在連接的設備或仿真器上運行程序,則可以在logcat中看到日誌。在Android Studio底部欄中搜索「Android Monitor」。 –

回答

1

只是initate 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();

+0

我修正了,現在我有一個單獨的問題。最初變量爲0,但在設置它們之後,關閉應用程序並重新打開它,它們顯示「android.support.v7.widget ... app:id/targethigh」是此參考ID,而不是值?而我將如何轉換爲實際值? –

+0

@BenRoux,已更新 – Vyacheslav

0

嘗試實例化您的onCreate方法中你mPrefs。

@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); 
    ... 
    ...