dialogBuilder.setMessage 为空

dialogBuilder.setMessage is empty

dialogBuilder.setMessage 什么都没显示!就好像它正在显示一个 null 字符串。但是,newUserPassword 更新为用户通过 EditText newPassword

的更新输入
public class OKActivity extends Activity {

EditText newPassword;
String newUserPassword;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ok);

    newPassword = (EditText) findViewById(R.id.new_password);
    newUserPassword = newPassword.getText().toString();
    getpasswordSharedPreferences();

    Button changePassword = (Button) findViewById(R.id.button_change);

    changePassword.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            setSharedPreferences();

        }
    });

}


private String getpasswordSharedPreferences() {

    SharedPreferences userPassword = getSharedPreferences("USER_PASSWORD", MODE_PRIVATE);
    String password = userPassword.getString("THE_PASSWORD", "123");
    return password;

}

private void setSharedPreferences() {
    SharedPreferences userPassword = getSharedPreferences("USER_PASSWORD", MODE_PRIVATE);
    SharedPreferences.Editor password_edior = userPassword.edit();
    password_edior.putString("THE_PASSWORD", newUserPassword);
    password_edior.commit();
    Toast.makeText(OKActivity.this, "Password Change Succesful", Toast.LENGTH_SHORT).show();
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(OKActivity.this);
        dialogBuilder.setIcon(R.drawable.ic_launcher);
        dialogBuilder.setTitle("Done!");
        dialogBuilder.setMessage(newUserPassword);
        dialogBuilder.setPositiveButton("OK", null);
        dialogBuilder.show();
}



试试这个:

public class OKActivity extends Activity {

EditText newPassword;
String newUserPassword;

@Override 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ok);

    newPassword = (EditText) findViewById(R.id.new_password);
    //do not retrieve password here
    Button changePassword = (Button) findViewById(R.id.button_change);

    changePassword.setOnClickListener(new OnClickListener() {

        @Override 
        public void onClick(View arg0) {
            //retrieve password here. Assuming user has entered some string before clicking on changePassword button
            newUserPassword = newPassword.getText().toString();
            getpasswordSharedPreferences(); 
            setSharedPreferences(); 

        } 
    }); 

} 
private String getpasswordSharedPreferences() {

    SharedPreferences userPassword = getSharedPreferences("USER_PASSWORD", MODE_PRIVATE);
    String password = userPassword.getString("THE_PASSWORD", "123");
    return password;

} 

private void setSharedPreferences() { 
    SharedPreferences userPassword = getSharedPreferences("USER_PASSWORD", MODE_PRIVATE);
    SharedPreferences.Editor password_edior = userPassword.edit();
    password_edior.putString("THE_PASSWORD", newUserPassword);
    password_edior.commit();
    Toast.makeText(OKActivity.this, "Password Change Succesful", Toast.LENGTH_SHORT).show();
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(OKActivity.this);
        dialogBuilder.setIcon(R.drawable.ic_launcher);
        dialogBuilder.setTitle("Done!");
        dialogBuilder.setMessage(newUserPassword);
        dialogBuilder.setPositiveButton("OK", null);
        dialogBuilder.show();
}