Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法通过Android中的SharedReferences保存数据_Java_Android_Textview_Sharedpreferences - Fatal编程技术网

Java 无法通过Android中的SharedReferences保存数据

Java 无法通过Android中的SharedReferences保存数据,java,android,textview,sharedpreferences,Java,Android,Textview,Sharedpreferences,我无法保存textView中的数据,当我再次打开应用程序时,我在textView中遇到错误: 错误消息字段XXXXX应为数字 我认为它没有保存任何数据,或者我使用了错误的方法。提前谢谢你 class MyJavaScriptInterface { @JavascriptInterface @SuppressWarnings("unused") public void processHTML(final String html) {

我无法保存
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", ""));
                    }

                }
    });
        }
    }
试试上面的代码


请尝试上面的代码

您在检查条件后很晚才启动SharedReference。所以首先要解决这个问题

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

            }
       });
    }
}

检查条件后,您延迟启动SharedReference。所以首先要解决这个问题

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

            }
       });
    }
}

你能提供完整的错误堆栈吗?为什么javascript标记在这里?请检查此项我假设你的editText的inputType不是文本,尝试添加到你的editText属性android:inputType=“text”你能提供完整的错误堆栈吗?为什么javascript标记在这里?请检查此项我假设你的editText的inputType不是文本,尝试将android:inputType=“text”添加到您的editText属性中,非常感谢@DHAVAL ASODARIYA的出色工作!非常感谢@DHAVAL ASODARIYA工作得很好!