尝试调用接口方法' android.content.SharedPreferences $ Editor.putString(java.lang.String,java.lang.String)'在空对象引用上 [英] Attempt to invoke interface method 'android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference

查看:97
本文介绍了尝试调用接口方法' android.content.SharedPreferences $ Editor.putString(java.lang.String,java.lang.String)'在空对象引用上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存首选项,以便当用户键入名称并单击保存"时,它会发布一个警告对话框,如果他们回答是",则会将其带到欢迎页面.但是,如果他们返回首页,则无需再次输入name,只需说(WELCOME,USER)即可.但是它给我一个错误,一个空异常错误.请帮助! 另外,我需要应用程序保存用户名,因此,当用户单击后退"按钮,然后转到第一页尝试再次输入其名称时,它将名称推入欢迎页面,因为该名称以前是类型 我将只添加与问题相关的代码片段.

I am trying to save preferences so when user types in name and clicks save, it releases an alert dialog and if they say yes, it takes them to a welcome page. however, if they go back to the first page, they do not need to type name in again, it'll just say (WELCOME, USER). however its giving me an error, a null exception error.Please help! Also, I would need for the app to save the username, so when the user clicks the back button, and then goes to the first page to try and type in their names again, it just pushes them to the welcome page, since the name has been types before I will just add snippets of my code relevant to the question.

这是用户输入名称的页面;

this is the page where the user types in name;

  import android.app.Activity;
    import android.app.AlertDialog.Builder;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.app.AlertDialog;
    import android.widget.EditText;

    public class userPreferences extends Activity {
    EditText editText;
    Button save;
    public static final String NAME="UserName";
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    String n;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.userpreferences);
        editText=(EditText)findViewById(R.id.editText);
        save=(Button)findViewById(R.id.button);
        sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
      save.setOnClickListener(new View.OnClickListener() {
          final AlertDialog.Builder alertDialog = new AlertDialog.Builder(userPreferences.this);

          @Override
          public void onClick(View arg0) {

                      alertDialog.setTitle("Alert Dialog");
              alertDialog.setMessage("Are you sure you want to save?");
              alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              n=editText.getText().toString();
                              sharedPreferences=getPreferences(MODE_PRIVATE);
                              editor=sharedPreferences.edit();
                              editor.putString(NAME,n);
                              editor.apply();
                              startActivity(new Intent(userPreferences.this,Welcome.class));

                          }
                      });
                      alertDialog.setNegativeButton("No", null);
                      alertDialog.show();


          }
      });

    }


}

这是欢迎页面;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Welcome extends Activity {

TextView textView;
SharedPreferences sharedPreferences;
public static final String NAME="UserName";
SharedPreferences.Editor editor;
String n;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);
    textView=(TextView)findViewById(R.id.textView);
    textView.setText("Welcome," + editor.putString(NAME, n));
    sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
    editor.apply();

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    startActivity(new Intent(Welcome.this, MainActivity.class));
    return;


}

逻辑读取中的错误

Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences$Editor.putString(java.lang.String, java.lang.String)' on a null object reference

请帮助!

推荐答案

更改

sharedPreferences=getPreferences(MODE_PRIVATE);

sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
if(!sharedPreferences.getString(NAME,"Default value").equals("Default value")){
  startActivity(new Intent(userPreferences.this, Welcome.class));
  finish();
}

也在您的欢迎班上

sharedPreferences=getSharedPreferences(NAME, Context.MODE_PRIVATE);
textView=(TextView)findViewById(R.id.textView);
textView.setText("Welcome," + sharedPreferences.getString(NAME,"Default value"));
editor = = sharedPreferences.edit();
editor.putString(NAME, sharedPreferences.getString(NAME,"Default value"));
editor.apply();

这篇关于尝试调用接口方法' android.content.SharedPreferences $ Editor.putString(java.lang.String,java.lang.String)'在空对象引用上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆