【问题标题】:Unable to save data via SharedPreferences in Android无法通过 Android 中的 SharedPreferences 保存数据
【发布时间】:2020-01-22 12:19:17
【问题描述】:

当我再次打开应用程序时,我无法保存来自 textView 的数据,我在 textview 中遇到错误:

错误信息字段 XXXXX 应该是数字

我认为它没有保存任何数据,或者我使用了不正确的方法。先感谢您!

  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", ""));
                    }

                }
    });
        }
    }

【问题讨论】:

  • 你能提供完整的错误堆栈吗?
  • 为什么这里有javascript标签?
  • 我想你的editText的inputType不是文本,尝试添加到你的editText属性android:inputType="text"

标签: java android textview sharedpreferences


【解决方案1】:

您在检查条件后延迟启动 sharedPreference。所以先解决这个问题。

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", ""));
                }

            }
       });
    }
}

【讨论】:

  • 非常感谢@DHAVAL ASODRIYA 完美运行!
【解决方案2】:
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", ""));
                }

            }
});
    }
}

试试上面的代码

【讨论】:

    猜你喜欢
    • 2021-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    相关资源
    最近更新 更多