如果在SharedPreferences
中可用,我的Activity
應顯示用戶的名稱和郵件地址。如果沒有Dialog
被啓動,其向用戶請求信息。帶有用戶輸入的AlertDialog在單擊確定按鈕時顯示NullPointerException
的Dialog
彈出,但點擊OK按鈕活動時拋出一個NullPointerException
與以下日誌,這是指向else
聲明:
10-29 10:36:59.745: E/AndroidRuntime(16518): FATAL EXCEPTION: main
10-29 10:36:59.745: E/AndroidRuntime(16518): java.lang.NullPointerException
10-29 10:36:59.745: E/AndroidRuntime(16518): at de.blubb.blubb.Blubb$1.onClick(Blubb.java:79)
10-29 10:36:59.745: E/AndroidRuntime(16518): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
10-29 10:36:59.745: E/AndroidRuntime(16518): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 10:36:59.745: E/AndroidRuntime(16518): at android.os.Looper.loop(Looper.java:137)
10-29 10:36:59.745: E/AndroidRuntime(16518): at android.app.ActivityThread.main(ActivityThread.java:4759)
10-29 10:36:59.745: E/AndroidRuntime(16518): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 10:36:59.745: E/AndroidRuntime(16518): at java.lang.reflect.Method.invoke(Method.java:511)
10-29 10:36:59.745: E/AndroidRuntime(16518): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
10-29 10:36:59.745: E/AndroidRuntime(16518): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
10-29 10:36:59.745: E/AndroidRuntime(16518): at dalvik.system.NativeStart.main(Native Method)
我的代碼:
public class Blubb extends Activity {
public static final String username = "nameKey"; // for Shared Preferences
public static final String mailaddress = "mailKey"; // for Shared Preferences
public static final String BlubbPrefs = "preferences" ;
private static final int DIALOG_ALERT = 10; // constant for identifying the preference dialog
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//addPreferencesFromResource(R.xml.preferences);
setContentView(R.layout.activity_blubb);
TextView viewUserName = (TextView) findViewById(R.id.viewUserNamen);
TextView viewUserMail = (TextView) findViewById(R.id.viewMailAdresse);
SharedPreferences settings = getSharedPreferences(BlubbPrefs, Context.MODE_PRIVATE);
String name = settings.getString(username, "");
String mail = settings.getString(mailaddress, "");
/* Test if user settings are available */
if ("".equals(name) || !"".equals(mail)) {
System.out.println("Nutzerangaben fehlen");
/* Requesting user settings with a Dialog */
showDialog(DIALOG_ALERT);
}
if (settings.contains(username)){
viewUserName.setText(settings.getString(username, ""));
}
if (settings.contains(mailaddress)){
viewUserMail.setText(settings.getString(mailaddress, ""));
}
}
public Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_ALERT:
/* Create the new Dialog. */
Context context = Blubb.this;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
//Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
builder.setView(inflater.inflate(R.layout.dialog_settings, null));
builder.setCancelable(false);
/* OK-Button */
builder.setPositiveButton(R.string.button_send, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
final EditText editName = (EditText)findViewById(R.id.editUserNamen);
final EditText editMail = (EditText)findViewById(R.id.editMailAdresse);
/* Prove user settings */
if (editName.getText().toString().trim().length() <= 0){
editName.setError("Name missing");
Toast.makeText(getApplicationContext(), "Name missing", Toast.LENGTH_LONG).show();
//showAlertNameMissing();
}
/*if (!inputMailAdresse.contains(".") || !inputMailAdresse.contains("@")) { */
if (editMail.getText().toString().trim().contains(".") || editMail.getText().toString().trim().contains("@")){
editMail.setError("Mail incorrect");
Toast.makeText(getApplicationContext(), "Mail incorrect", Toast.LENGTH_LONG).show();
//showAlertMailMissing();
}
else {
final String inputUserNamen = editName.getText().toString().trim();
final String inputMailAdresse = editMail.getText().toString().trim();
/* Reading access on preferences.xml */
SharedPreferences settings = getSharedPreferences(BlubbPrefs, Context.MODE_PRIVATE); // MODE_PRIVATE => can only be accessed by this application
/* Object of type editor to get writing access on preferences.xml */
SharedPreferences.Editor editor = settings.edit();
/* Saving key/value pairs to preferences.xml */
editor.putString(username, inputUserNamen);
editor.putString(mailaddress, inputMailAdresse);
/* Writing key/value pairs to preferences.xml */
editor.commit();
Toast.makeText(getApplicationContext(), R.string.alert_saved, Toast.LENGTH_SHORT).show();
//return;
}
dialog.dismiss();
}
}
);
/* Cancel-Button */
builder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Input cancelled", Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
return super.onCreateDialog(id);
}
}
作爲編程的新手,當然還有Java,我一直試圖通過搜索互聯網和改變我的代碼來找到2周的解決方案。但是現在我很困惑。
解決了這個問題後我還會遇到Dialog關閉問題here?有誰能夠幫助我?
試試這個AlertDialog.Builder builder = new AlertDialog.Builder(Blubb.this); – raj 2014-10-29 10:41:54
EditText屬於哪個佈局? – 2014-10-29 10:43:15
你不應該在類中初始化變量。只是聲明它們並在oncreate方法初始化.. – 2014-10-29 10:50:42