我的Android应用程序的代码如下,我使用sharedpreferences编写。我从stackexchange获取了所有信息,但我仍然无法获得保存的值以显示在屏幕上。这是我的代码:
package com.example.shivamgandhi.gyrosafe;
import android.content.Intent;
import android.os.Bundle;
import android.content.Context;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.app.Activity;
import android.view.MenuItem;
import android.util.Log;
import android.widget.Button;
import android.content.SharedPreferences;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.TextView;
import org.w3c.dom.Text;
public class settings_page_activity extends AppCompatActivity implements View.OnClickListener{
EditText ed1, ed2;
Button btnArray2[] = new Button[5];
public static final String MyPREFERENCES = "MyPrefs";
public static final String Phone = "phoneKey";
public static final String Email = "emailKey";
SharedPreferences sharedpreferences;
SharedPreferences.Editor preferenceEditor;
private static final int PREFERENCE_MODE_PRIVATE = 0;
TextView textView;
TextView textView2;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_page);
btnArray2[0] = (Button)findViewById(R.id.button3);
btnArray2[1] = (Button)findViewById(R.id.button4);
btnArray2[2] = (Button)findViewById(R.id.button5);
btnArray2[3] = (Button)findViewById(R.id.button6);
btnArray2[4] = (Button)findViewById(R.id.button12);
for (int i = 0; i <4; i++){
btnArray2[i].setOnClickListener(this);
}
sharedpreferences = this.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
ed1 = (EditText)findViewById(R.id.editText);
ed2 = (EditText)findViewById(R.id.editText2);
textView = (TextView)findViewById(R.id.textView41);
textView.setText(sharedpreferences.getString("Email", "No Email"));
textView2 = (TextView)findViewById(R.id.textView42);
textView2.setText(sharedpreferences.getString("Phone", "No Phone"));
}
@Override
public void onClick(View v) {
if(v == findViewById(R.id.button3)){
Intent myIntent = new Intent(this, MainActivity.class );
startActivity(myIntent);
}
else if(v == findViewById(R.id.button4)){
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
else if(v == findViewById(R.id.button5)){
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
else if(v == findViewById(R.id.button6)){
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
else if(v == findViewById(R.id.button12)){
String e = ed1.getText().toString();
String ph = ed2.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
if(sharedpreferences.getString("Email", "No Email") != "No Email"){
sharedpreferences.edit().remove("Email").apply();
}
if(sharedpreferences.getString("Phone", "No Phone") != "No Phone"){
sharedpreferences.edit().remove("Phone").apply();
}
editor.putString("Email", e);
editor.putString("Phone", ph);
editor.commit();
Toast.makeText(settings_page_activity.this,"Thanks",Toast.LENGTH_LONG).show();
}
}
}
这是我的应用程序的设置页面。我希望用户能够输入电话号码和电子邮件地址,然后按button12保存这两个值。保存这些值后,每当用户返回页面时,我都希望电子邮件地址和电话号码显示在设置页面上,这通过textView和textView2完成。
如果我写的信息不够,请告诉我,以便澄清。谢谢高级:)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/Settings_Page"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Begin_Calibration"
android:id="@+id/button3"
android:layout_marginTop="31dp"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/View_Baseline_Results"
android:id="@+id/button4"
android:layout_below="@+id/button3"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Test_Design_Summary"
android:id="@+id/button5"
android:layout_alignTop="@+id/button4"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Email_Results_To"
android:id="@+id/textView2"
android:layout_marginStart="24dp"
android:layout_below="@+id/button4"
android:layout_alignParentStart="true"
android:layout_marginTop="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/Emergency_Number"
android:id="@+id/textView3"
android:layout_marginTop="57dp"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/editText"
android:layout_below="@+id/textView2"
android:layout_alignStart="@+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="@+id/editText2"
android:layout_below="@+id/textView3"
android:layout_alignStart="@+id/textView3" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Home"
android:id="@+id/button6"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/textView"
android:layout_marginStart="26dp"
android:layout_marginBottom="41dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Save_Information"
android:id="@+id/button12"
android:layout_above="@+id/button6"
android:layout_toStartOf="@+id/button5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView42"
android:layout_gravity="right"
android:layout_above="@+id/editText"
android:layout_alignEnd="@+id/editText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textView43"
android:layout_above="@+id/editText2"
android:layout_toEndOf="@+id/button12" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView41"
android:layout_gravity="right" />
</LinearLayout>
答案 0 :(得分:0)
这看起来像是一个经典的竞争条件。您正在混合同步和异步调用以修改共享首选项。
editor.commit();
同步意味着它会立即应用,而后台执行sharedpreferences.apply()
,因此您可能会调用sharedpreferences.edit().remove("Phone").apply();
和在调用sharedpreferences.edit().remove("Email").apply();
editor.commit();
正在执行
如果这不能解决您的问题,那么当您按下按钮12时,您的TOAST会出现吗? v == findViewById(R.id.button12)
的等式检查有可能失败,在这种情况下,&#34;首选&#34;这样做的方法是使用v.getId() == R.id.button12
进行检查。这种情况并不常见,但在直接比较视图时我听说过这种等式检查失败
<强> 修改 强>
另外,应该注意的是你在上面的代码中创建了对编辑器的引用之后调用prefs.edit()。remove()。apply()(以后也使用editor.stuff()),这也是冗余编码,应该解决,但这与你的实际问题没有密切关系
btnArray2[0] = (Button)findViewById(R.id.button3); btnArray2[1] = (Button)findViewById(R.id.button4); btnArray2[2] = (Button)findViewById(R.id.button5); btnArray2[3] = (Button)findViewById(R.id.button6); btnArray2[4] = (Button)findViewById(R.id.button12); for (int i = 0; i <4; i++){ btnArray2[i].setOnClickListener(this); }
您的循环条件i<4
表示setOnClickListener
没有为btnArray2[4]
调用,这意味着您的button12永远不会获得OnClickListener集。你的循环条件应该是
for (int i = 0; i < btnArray2.length; i++)