我试图保存首选项,所以当用户输入名称并单击保存时,它会释放一个警告对话框,如果他们说是,它会将他们带到欢迎页面。然而,如果他们回到第一页,他们不需要再次输入名称,它只会显示(WELCOME, USER)。然而,它给了我一个错误,一个空异常错误。请帮助!此外,我需要的应用程序来保存用户名,所以当用户点击后退按钮,然后去到第一页尝试再次输入他们的名字,它只是把他们推到欢迎页面,因为名字已经类型之前我将只添加与问题相关的代码片段。
这是用户输入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();
try:
// 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();
另外,您正在尝试使用编辑器。putString(名称、n);这不会从首选项中读取:
sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);
String name = sharedPreferences.getString(NAME);
textView.setText("Welcome," + name);
为了保存这个用户名以备下次使用,您必须调用编辑器并在再次读取它之后保存它,在一个空对象上调用调用editor.apply()。这是您的onCreate()
的完整工作样本:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
textView=(TextView)findViewById(R.id.textView);
sharedPreferences = getSharedPreferences(NAME, MODE_PRIVATE);
String name = sharedPreferences.getString(NAME);
textView.setText("Welcome," + name);
editor = sharedPreferences.edit();
editor.putString(NAME, name);
editor.apply();
}
问题在于,首先,您没有在Welcome类中初始化editor
对象,因此它是空的。
但是,即使您初始化它,您也不会得到所需的行为,因为编辑器用于编辑SharedPreferences,而不能用于检索首选项。editor.putString(NAME, n)
存储一个字符串并返回一个布尔值,它没有得到用户的名字。它也是空的,因为你没有初始化它。
您甚至不需要在Welcome类中使用Editor
对象,相反,您需要使用sharedPreferences
变量,如下所示:
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
}