Android 自定义警报对话框中的共享首选项
我想在Android 自定义警报对话框中的共享首选项,android,sharedpreferences,android-alertdialog,Android,Sharedpreferences,Android Alertdialog,我想在SharedReference中存储EditText(在AlertDialog中)字段的值。它在这一行显示NullPointerExceptionSharedReferences.Editor=sp.edit()**注意:代码在没有SharedReference的情况下工作** private void myDialog() { // TODO Auto-generated method stub LayoutInflater inflater = LayoutIn
SharedReference
中存储EditText
(在AlertDialog
中)字段的值。它在这一行显示NullPointerException
SharedReferences.Editor=sp.edit()代码>**注意:代码在没有SharedReference的情况下工作**
private void myDialog() {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
View view = inflater.inflate(R.layout.cus_dialog, null);
AlertDialog.Builder ab = new AlertDialog.Builder(this);
ab.setView(view);
final EditText input = (EditText) view.findViewById(R.id.etValue);
ab.setCancelable(false);
ab.setPositiveButton("Send", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String s = input.getText().toString();
SharedPreferences.Editor editor = sp.edit();
editor.putString("A", s);
editor.commit();
Toast.makeText(getApplicationContext(), "" + s,
Toast.LENGTH_SHORT).show();
}
});
ab.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog ad = ab.create();
ad.show();
}
您的sp变量在哪里声明?
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(acivity.this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("a", "a");
editor.commit();