Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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
尝试调用接口方法';android.content.SharedReferences$Editor.putString(java.lang.String,java.lang.String)和#x27;关于空对象引用_Android_Android Activity_Sharedpreferences - Fatal编程技术网

尝试调用接口方法';android.content.SharedReferences$Editor.putString(java.lang.String,java.lang.String)和#x27;关于空对象引用

尝试调用接口方法';android.content.SharedReferences$Editor.putString(java.lang.String,java.lang.String)和#x27;关于空对象引用,android,android-activity,sharedpreferences,Android,Android Activity,Sharedpreferences,我正在尝试保存首选项,所以当用户键入name并单击save时,它会释放一个警报对话框,如果他们说是,它会将他们带到欢迎页面。然而,如果他们返回到第一页,他们不需要再次输入名称,它只会说(欢迎,用户)。但是它给了我一个错误,一个空异常错误。请帮助! 此外,我还需要应用程序保存用户名,因此当用户单击“上一步”按钮,然后转到第一页尝试再次键入他们的姓名时,它只会将他们推到欢迎页面,因为以前已经键入过姓名 我将只添加与问题相关的代码片段 这是用户键入名称的页面 import android.app.

我正在尝试保存首选项,所以当用户键入name并单击save时,它会释放一个警报对话框,如果他们说是,它会将他们带到欢迎页面。然而,如果他们返回到第一页,他们不需要再次输入名称,它只会说(欢迎,用户)。但是它给了我一个错误,一个空异常错误。请帮助! 此外,我还需要应用程序保存用户名,因此当用户单击“上一步”按钮,然后转到第一页尝试再次键入他们的姓名时,它只会将他们推到欢迎页面,因为以前已经键入过姓名 我将只添加与问题相关的代码片段

这是用户键入名称的页面

  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
请帮忙

试试看:

// open your preferences using the name you originally used to create it
sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);

// open for editing
editor = sharedPreferences.edit();

// you probably don't want to use the same name here as preferences name 
editor.putString(NAME,n);
editor.apply();
此外,您正在尝试使用editor.putString(名称,n);这不从首选项读取,请执行以下操作:

sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);
String name = sharedPreferences.getString(NAME);

textView.setText("Welcome," + name);
更新 为了保存此用户名以备下次使用,您必须调用编辑器,并在再次读取该用户名后对空对象调用调用编辑器.apply()保存该用户名。以下是您的
onCreate()
的完整工作示例:

sharedPreferences=getPreferences(MODE_PRIVATE);

也在欢迎课上

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();

问题是,首先,您没有初始化Welcome类中的
编辑器
对象,因此它是空的

但是,即使对其进行初始化,也不会获得所需的行为,因为编辑器用于编辑SharedReference,而不能用于检索首选项
editor.putString(NAME,n)
存储一个字符串并返回一个布尔值,它不获取用户名。它也是空的,因为您尚未初始化它

您甚至不需要Welcome类中的
编辑器
对象,而是需要使用
SharedReferences
变量,如下所示:

TextView textView;
SharedPreferences sharedPreferences;
public static final String NAME = "UserName";
public static final String defaultName = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);
    textView = (TextView) findViewById(R.id.textView);

    sharedPreferences = getSharedPreferences(NAME, Context.MODE_PRIVATE);
    textView.setText("Welcome," + sharedPreferences.getString(NAME, defaultName));
}
编辑:如果用户已经输入用户名,您想跳过登录活动,那么您需要检查他们是否输入了用户名,然后直接从您的登录活动启动该活动

EditText editText;
Button save;
public static final String NAME="UserName";
public static final String default = "";
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);
    if (sharedPreferences.getString(NAME, default).length() > 0) {
        // The user has already entered a username, so start the Welcome activity
        startActivity(new Intent(userPreferences.this, Welcome.class));
        finish();
    }
    // put the rest of your onCreate code here
}

它不为null,应用程序在到达欢迎页面之前崩溃,它似乎是一行textView.setText(“欢迎,+editor.putString(NAME,n));导致它崩溃,但我不知道why@AriaMontgomery请参阅更新的答案您不能使用编辑器读取用户名代码正常工作,但它不会为下一步保存用户名time@AriaMontgomery您必须保存该名称,以便再次阅读。在updateit下尝试上面的方法,但仍然无法保存它。我有一个onBackPressed方法,它使back按钮跳过登录页面,直接转到第一页,但是当我到达第一页,单击登录页面的按钮时,它只会显示一个新的登录页面,而不是欢迎页面。代码工作得非常好!然而,输入用户名后,我如何让它记住用户名?@AriaMontgomery我查看了您的
userPreferences
活动,其中的代码看起来应该可以工作。查看是否可以在
userPreferences
活动中更改它,如果看到更改,请告诉我。它仍然无法保存。也许我应该补充一点,我有一个onBackPressed方法,它让后退按钮跳过登录页面,直接进入第一页,但是当我进入第一页,点击登录页面的按钮时,它只会显示一个新的登录页面,而不是欢迎页面page@AriaMontgomery哦,我明白了,如果用户输入了用户名,则希望跳过登录活动。。。让我更改您的登录活动。@AriaMontgomery我已更新了登录活动,以便在用户输入用户名时打开欢迎屏幕。让我知道它是否适用于您,并且不要忘记在if块之后添加
AlertDialog
代码,以便登录对话框将显示他们是否尚未登录。它会运行,但不会保存用户名。它仍然不会保存。我有一个onBackPressed方法,它使后退按钮跳过登录页面,直接转到第一页,但是当我到达第一页,点击登录页面的按钮时,它只会显示一个新的登录页面,而不是欢迎页面。不,我希望应用程序已经记住名称,只需将用户发送到欢迎页面
TextView textView;
SharedPreferences sharedPreferences;
public static final String NAME = "UserName";
public static final String defaultName = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);
    textView = (TextView) findViewById(R.id.textView);

    sharedPreferences = getSharedPreferences(NAME, Context.MODE_PRIVATE);
    textView.setText("Welcome," + sharedPreferences.getString(NAME, defaultName));
}
EditText editText;
Button save;
public static final String NAME="UserName";
public static final String default = "";
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);
    if (sharedPreferences.getString(NAME, default).length() > 0) {
        // The user has already entered a username, so start the Welcome activity
        startActivity(new Intent(userPreferences.this, Welcome.class));
        finish();
    }
    // put the rest of your onCreate code here
}