我遇到了一个奇怪的问题,我在xml中只有EditText
个框,我给它们提供了默认值。我也在使用共享首选项,其中我保存用户输入的值。
问题是在模拟器中EditText
工作正常但在实际物理设备上的值是空的,我错过了什么?
<EditText
android:id="@+id/etTRQPO"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
android:singleLine="true"
android:text="15">
</EditText>
java代码
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Getacc extends Activity {
Button save6;
EditText edtTRQPO, edtTRQGE, edtTRQFW, edtTRQGR, edtTRQBN, edtTRQLT,
edtTRQPP, edtTRQCG;
int tV, tW, tX, counterAC;
String tsTRQPO, tsTRQGE, tsTRQFW, tsTRQGR, tsTRQBN, tsTRQLT, tsTRQPP,
tsTRQCG;
public static String FILE1 = "MyPrefsFile";
SharedPreferences abcPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.getacc);
save6 = (Button) findViewById(R.id.btGoresult);
edtTRQPO = (EditText) findViewById(R.id.etTRQPO);
edtTRQGE = (EditText) findViewById(R.id.etTRQGE);
edtTRQFW = (EditText) findViewById(R.id.etTRQFW);
edtTRQGR = (EditText) findViewById(R.id.etTRQGR);
edtTRQBN = (EditText) findViewById(R.id.etTRQBN);
edtTRQLT = (EditText) findViewById(R.id.etTRQLT);
edtTRQPP = (EditText) findViewById(R.id.etTRQPP);
edtTRQCG = (EditText) findViewById(R.id.etTRQCG);
abcPref = getSharedPreferences(FILE1, 0);
edtTRQPO.setText(abcPref.getString("tsTRQPO", ""));
edtTRQGE.setText(abcPref.getString("tsTRQGE", ""));
edtTRQFW.setText(abcPref.getString("tsTRQFW", ""));
edtTRQGR.setText(abcPref.getString("tsTRQGR", ""));
edtTRQBN.setText(abcPref.getString("tsTRQBN", ""));
edtTRQLT.setText(abcPref.getString("tsTRQLT", ""));
edtTRQPP.setText(abcPref.getString("tsTRQPP", ""));
edtTRQCG.setText(abcPref.getString("tsTRQCG", ""));
save6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if ((!edtTRQPO.getText().toString().equals(""))
&& (!edtTRQGE.getText().toString().equals(""))
&& (!edtTRQFW.getText().toString().equals(""))
&& (!edtTRQGR.getText().toString().equals(""))
&& (!edtTRQBN.getText().toString().equals(""))
&& (!edtTRQLT.getText().toString().equals(""))
&& (!edtTRQPP.getText().toString().equals(""))
&& (!edtTRQCG.getText().toString().equals(""))) {
// TODO Auto-generated method stub
counterAC =1;
abcPref = getSharedPreferences(FILE1, 0);
SharedPreferences.Editor editor = abcPref.edit();
editor.putString("tsTRQPO", edtTRQPO.getText().toString());
editor.putString("tsTRQGE", edtTRQGE.getText().toString());
editor.putString("tsTRQFW", edtTRQFW.getText().toString());
editor.putString("tsTRQGR", edtTRQGR.getText().toString());
editor.putString("tsTRQBN", edtTRQBN.getText().toString());
editor.putString("tsTRQLT", edtTRQLT.getText().toString());
editor.putString("tsTRQPP", edtTRQPP.getText().toString());
editor.putString("tsTRQCG", edtTRQCG.getText().toString());
editor.putInt("counterac", counterAC);
editor.commit();
Toast message = Toast.makeText(Getacc.this,
"Values are saved", 2000);
message.setGravity(Gravity.BOTTOM, 0, 0);
message.show();
Intent opentime = new Intent("com.sports.sport.TIME");
startActivity(opentime);
onPause();
} else {
Toast failz = Toast.makeText(Getacc.this,
"Values are not Entered", 2000);
failz.setGravity(Gravity.BOTTOM, 0, 0);
failz.show();
}
};
});
}
}
答案 0 :(得分:1)
首次在真实设备上运行app时,它将显示默认值,而不是SharedPreferences中的值,因为SharedPreferences中的值为空。如果SharedPreferences具有空值,当您打开第二个应用程序时,EditText将显示SharedPreferences中的空值。您需要应用一些值检查,如
if(user_entered_values_in_edittext){
//Store values in SharedPreferences otherwise not.
}
// On app resume - inside onResume() or onCreate()
if(SharedPreferences have empty value or no value){
//Show your EditText default value that you have defined in xml file.
}
更正了Getacc.java文件...
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Getacc extends Activity {
Button save6;
EditText edtTRQPO, edtTRQGE, edtTRQFW, edtTRQGR, edtTRQBN, edtTRQLT,
edtTRQPP, edtTRQCG;
int tV, tW, tX, counterAC;
String tsTRQPO, tsTRQGE, tsTRQFW, tsTRQGR, tsTRQBN, tsTRQLT, tsTRQPP,
tsTRQCG;
public static String FILE1 = "MyPrefsFile";
SharedPreferences abcPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.getacc);
save6 = (Button) findViewById(R.id.btGoresult);
edtTRQPO = (EditText) findViewById(R.id.etTRQPO);
edtTRQGE = (EditText) findViewById(R.id.etTRQGE);
edtTRQFW = (EditText) findViewById(R.id.etTRQFW);
edtTRQGR = (EditText) findViewById(R.id.etTRQGR);
edtTRQBN = (EditText) findViewById(R.id.etTRQBN);
edtTRQLT = (EditText) findViewById(R.id.etTRQLT);
edtTRQPP = (EditText) findViewById(R.id.etTRQPP);
edtTRQCG = (EditText) findViewById(R.id.etTRQCG);
abcPref = getSharedPreferences(FILE1, 0);
// First get All values stored in SharedPreferences
tsTRQPO = abcPref.getString("tsTRQGE", null);
tsTRQGE = abcPref.getString("tsTRQGE", null);
tsTRQFW = abcPref.getString("tsTRQFW", null);
tsTRQGR = abcPref.getString("tsTRQGR", null);
tsTRQBN = abcPref.getString("tsTRQBN", null);
tsTRQLT = abcPref.getString("tsTRQLT", null);
tsTRQPP = abcPref.getString("tsTRQPP", null);
tsTRQCG = abcPref.getString("tsTRQCG", null);
// Check if values are not null
if(tsTRQPO != null && tsTRQGE != null && tsTRQFW != null && tsTRQGR!= null && tsTRQBN != null && tsTRQLT != null && tsTRQPP != null && tsTRQCG != null){
edtTRQPO.setText(tsTRQPO);
edtTRQGE.setText(tsTRQGE);
edtTRQFW.setText(tsTRQFW);
edtTRQGR.setText(tsTRQGR);
edtTRQBN.setText(tsTRQBN);
edtTRQLT.setText(tsTRQLT);
edtTRQPP.setText(tsTRQPP);
edtTRQCG.setText(tsTRQCG);
}else{
//Do nothing.. EditText will show Default values defined in xml file
}
save6.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if ((!edtTRQPO.getText().toString().equals(""))
&& (!edtTRQGE.getText().toString().equals(""))
&& (!edtTRQFW.getText().toString().equals(""))
&& (!edtTRQGR.getText().toString().equals(""))
&& (!edtTRQBN.getText().toString().equals(""))
&& (!edtTRQLT.getText().toString().equals(""))
&& (!edtTRQPP.getText().toString().equals(""))
&& (!edtTRQCG.getText().toString().equals(""))) {
// TODO Auto-generated method stub
counterAC =1;
abcPref = getSharedPreferences(FILE1, 0);
SharedPreferences.Editor editor = abcPref.edit();
editor.putString("tsTRQPO", edtTRQPO.getText().toString());
editor.putString("tsTRQGE", edtTRQGE.getText().toString());
editor.putString("tsTRQFW", edtTRQFW.getText().toString());
editor.putString("tsTRQGR", edtTRQGR.getText().toString());
editor.putString("tsTRQBN", edtTRQBN.getText().toString());
editor.putString("tsTRQLT", edtTRQLT.getText().toString());
editor.putString("tsTRQPP", edtTRQPP.getText().toString());
editor.putString("tsTRQCG", edtTRQCG.getText().toString());
editor.putInt("counterac", counterAC);
editor.commit();
Toast message = Toast.makeText(Getacc.this,
"Values are saved", 2000);
message.setGravity(Gravity.BOTTOM, 0, 0);
message.show();
Intent opentime = new Intent("com.sports.sport.TIME");
startActivity(opentime);
onPause();
} else {
Toast failz = Toast.makeText(Getacc.this,
"Values are not Entered", 2000);
failz.setGravity(Gravity.BOTTOM, 0, 0);
failz.show();
}
};
});
}
}
答案 1 :(得分:0)
共享首选项保存在缓存中。如果从IDE重新启动应用程序,则不会清除共享首选项。如果你想清除它,你应该
只有那时它才是空的。所以也许你的问题是你在模拟器和设备上有旧值它是新的(反之亦然)
答案 2 :(得分:0)
第一次,应用程序中没有SharedPreferences,因此abcPref.getString("tsTRQPO", "")
和其他人会导致此处给出的空默认值。通过填写所有编辑文本保存数据后,您将从下次开始正确处理数据。
要进行检查,请尝试从SharedPreferences获取数据时设置一些默认值。对于ex edtTRQPO.setText(abcPref.getString("tsTRQPO", "15"));
并检查它是否第一次加载值为15。
答案 3 :(得分:0)
这是因为你试图从SharedPreferences获取OnCreate方法的值,当前有空白意味着null,
现在来看,当你启动你的应用程序时,它将首先执行OnCreate方法,在你尝试从sharedPreferences获取值时,当你在save6.setOnClickListener
上保存值时
是什么?
注意:在sharedPreferences中保存后获取值