簡體   English   中英

我想使用共享首選項保存,但應用程序崩潰

[英]I want to save with shared preference but the app crashes

我想制作一個應用程序,基本上用戶在里面輸入一些字符串

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal">

    <com.google.android.material.textfield.TextInputLayout

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:textColorHint="#ffffff"
        app:errorEnabled="true">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/texto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="#4249ff"
            android:hint="@string/BtnO"
            android:textColor="#ffffff" />
    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:onClick="confirmInput"
        android:text="@string/Btns" />

</LinearLayout>

因此,當用戶按下確認按鈕時,我希望它被保存,但應用程序崩潰,而不是我第一次使用共享首選項時,我並不真正理解它。我不保存用戶的值。另外我知道我使用了提示,但根據我的共享偏好,它將提示更改為文本,而不是保持原樣。 這是我的Java代碼:

public class MainActivity extends AppCompatActivity {
    private EditText nameInput;
    private EditText homeInput;
    private SharedPreferences prefs;

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

        SharedPreferences prefs= getSharedPreferences("my_data",MODE_PRIVATE);
        String name = prefs.getString("MY_NAME","name");
        String home = prefs.getString("MY_HOME","home");

        ((EditText)findViewById(R.id.texto)).setText(name);
        ((EditText)findViewById(R.id.textd)).setText(home);

        nameInput =(EditText)findViewById(R.id.texto);
        nameInput =(EditText)findViewById(R.id.textd);

    }

    public void copy(View view) {
    }

    public void confirmInput(View view) {
        String name=nameInput.getText().toString();
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("MY_NAME",name);
        editor.apply();
    }
    public void confirmInput2(View view) {
        String home=homeInput.getText().toString();
        SharedPreferences.Editor editor=prefs.edit();
        editor.putString("MY_HOME",home);
        editor.apply();
    }

}

我認為錯誤是因為SharedPreferences prefs有兩個聲明,一個在onCreate方法級別,另一個在類的開頭

只需刪除onCreate的 prefs 聲明並使用類開頭定義的變量即可修復該錯誤,如下所示

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Take a look here!
    this.prefs= getSharedPreferences("my_data",MODE_PRIVATE);
    String name = prefs.getString("MY_NAME","name");
    String home = prefs.getString("MY_HOME","home");

    ((EditText)findViewById(R.id.texto)).setText(name);
    ((EditText)findViewById(R.id.textd)).setText(home);

    nameInput =(EditText)findViewById(R.id.texto);
    nameInput =(EditText)findViewById(R.id.textd);

}

只需做這個小改動:

    prefs= 
         getSharedPreferences("my_data",MODE_PRIVATE);
         String name=prefs.getString("MY_NAME","name");

代替

      SharedPreferences prefs= 
       getSharedPreferences("my_data",MODE_PRIVATE);
       String name = prefs.getString("MY_NAME","name");

您已經全局定義了 sharedpref 對象。 您不需要再次創建它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM