[英]Unable to save data via SharedPreferences in Android
I'm unable to save data from textView
when I'm opening an app again I'm getting error in textview:当我再次打开应用程序时,我无法从
textView
保存数据 我在 textview 中遇到错误:
Error message Field XXXXX should be numeric
错误消息字段 XXXXX 应为数字
I think it's not saving any data or I'm using an incorrect method.我认为它没有保存任何数据,或者我使用了不正确的方法。 Thank you in advance!
先感谢您!
class MyJavaScriptInterface {
@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(final String html) {
myWebView.post(new Thread() {
@Override public void run() {
TextView text = findViewById(R.id.textView);
if(!sharedPreferences.contains("statusKey")){
sharedPreferences = getSharedPreferences("prefID", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("statusKey", html);
editor.apply();
text.setText(html);
}
else {
text.setText(sharedPreferences.getString("statusKey", ""));
}
}
});
}
}
class MyJavaScriptInterface {
@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(final String html) {
myWebView.post(new Thread() {
@Override public void run() {
TextView text = findViewById(R.id.textView);
sharedPreferences = getSharedPreferences("prefID", Context.MODE_PRIVATE);
if(!sharedPreferences.contains("statusKey")){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("statusKey", html);
editor.apply();
text.setText(html);
}
else {
text.setText(sharedPreferences.getString("statusKey", ""));
}
}
});
}
}
Try above code试试上面的代码
You initiating sharedPreference late after checking condition.您在检查条件后延迟启动 sharedPreference。 So first fix that.
所以先解决这个问题。
class MyJavaScriptInterface {
@JavascriptInterface
@SuppressWarnings("unused")
public void processHTML(final String html) {
myWebView.post(new Thread() {
@Override public void run() {
TextView text = findViewById(R.id.textView);
sharedPreferences = getSharedPreferences("prefID", Context.MODE_PRIVATE);
if(!sharedPreferences.contains("statusKey")){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("statusKey", html);
editor.apply();
text.setText(html);
}
else {
text.setText(sharedPreferences.getString("statusKey", ""));
}
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.