EditText中的文本未保存在“共享首选项”中

时间:2016-08-24 05:43:15

标签: android string android-edittext sharedpreferences

我有一个EditText,如果字符串不为空或null,我想在EditText中将文本保存为共享首选项中的字符串。但是我的应用程序崩溃了以下代码:

Activity.java:

Button buttonNext = (Button) findViewById(R.id.buttonNext);
    assert buttonNext != null;
    buttonNext.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            if (checkForGuardianNames()) {
                Log.i("Guardian names: ", "check passed");
                Log.i("ActivityEditText", inputGuardian1.getText().toString());
                Toast toast = Toast.makeText(UserInfoActivity.this, inputGuardian1.getText().toString(), Toast.LENGTH_LONG);
                toast.show();
                saveGuardianNames();

                } else {
                    promptToFillField("Guardian");
                    Log.i("Guardian names: ", "check failed");
//                    Toast toast = Toast.makeText(UserInfoActivity.this, "Works well part 2 "+ inputGuardian1.getText().toString(), Toast.LENGTH_LONG);
//                    toast.show();
                }
            }

现在我知道checkForGuardianNames()通过,但应用程序在调用saveGuardianNames()方法时崩溃。

我的saveGuardianNames()

private void saveGuardianNames(){
    String guardian1 = inputGuardian1.getText().toString();
    String guardian2 = inputGuardian2.getText().toString();
    String guardian3 = inputGuardian3.getText().toString();
    String guardian4 = inputGuardian4.getText().toString();
    Log.i("Value: ", "Dummy");
    Log.i("saveGuardianNames", "Called");


        if (!inputGuardian1.getText().toString().matches("")){
            Log.i("Putting text", "in sp");
            sharedPreferences.edit().putString("Guardian 1", guardian1).apply();
            Log.i("Putting text", "passed");
        }
}

现在我在logcat中看到saveGuardianNames: check passed所以方法被正确调用但是上面的行不是(Log.i("Value: ", "Dummy");)并且在LogCat中没有提到它。另外,根据我的理解,在Log.i("Putting text", "in sp");执行之前,if语句永远不会被调用或崩溃。

我的错误LogCat:

08-24 01:40:43.712 1497-1497/com.bluefirelabs.apptiva I/Guardian names:: check passed
08-24 01:40:43.732 1497-1497/com.bluefirelabs.apptiva I/saveGuardianNames: Called
08-24 01:40:43.732 1497-1497/com.bluefirelabs.apptiva E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.bluefirelabs.apptiva, PID: 1497
                                                                        java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
                                                                            at com.bluefirelabs.apptiva.UserInfoActivity$override.saveGuardianNames(UserInfoActivity.java:374)
                                                                            at com.bluefirelabs.apptiva.UserInfoActivity$override.static$access$200(UserInfoActivity.java:27)
                                                                            at com.bluefirelabs.apptiva.UserInfoActivity$override.access$dispatch(UserInfoActivity.java)
                                                                            at com.bluefirelabs.apptiva.UserInfoActivity.access$200(UserInfoActivity.java:0)
                                                                            at com.bluefirelabs.apptiva.UserInfoActivity$1$override.onClick(UserInfoActivity.java:112)
                                                                            at com.bluefirelabs.apptiva.UserInfoActivity$1$override.access$dispatch(UserInfoActivity.java)
                                                                            at com.bluefirelabs.apptiva.UserInfoActivity$1.onClick(UserInfoActivity.java:0)
                                                                            at android.view.View.performClick(View.java:5702)
                                                                            at android.widget.TextView.performClick(TextView.java:10885)
                                                                            at android.view.View$PerformClick.run(View.java:22533)
                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:158)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我有点感到困惑的是为什么有些行正在执行,而有些行在它之前没有执行,以及为什么应用程序在将任何内容保存到共享首选项之前崩溃。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:1)

if (!inputGuardian1.getText().toString().matches("")){
            Log.i("Putting text", "in sp");
          if(sharedPreferences != null){
            sharedPreferences.edit().putString("Guardian 1", guardian1).apply();
            Log.i("Putting text", "passed");
           }else{
            sharedPreferences = mContext.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
            sharedPreferences.edit().putString("Guardian 1", guardian1).apply();
           }
        }

答案 1 :(得分:0)

您的func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { guard let url = request.URL where url.host == "www.mywebsite.com" && url.path == "/instagram_auth_ios" else { return true } guard let authUrl = NSURLComponents(URL: url, resolvingAgainstBaseURL: false) else { return true } // Customize the scheme/host/path, etc. as desired for your app authUrl.scheme = "myappschema" authUrl.host = "instagram" authUrl.path = "" UIApplication.sharedApplication().openURL(authUrl.URL!) return false } 对象未初始化,因此请将其初始化为

如果您正在进行活动,那么就这样做

SharedPreferences

如果您正在使用Fragment,那么就这样做

SharedPreferences.Editor editor = getSharedPreferences("YouKeyName", MODE_PRIVATE).edit();

editor.putString("Guardian 1", "Hi");
editor.commit();

答案 2 :(得分:0)

showPage(page)