[英]Cannot save editText value in Android AlertDialog popup in OptionsMenu
我的代碼如下所示:單擊選項菜單,選擇“編輯提示率”。 然后,將出現一個包含文本字段,取消按鈕和保存按鈕的彈出窗口。 我在文本字段中輸入一個值,然后單擊“保存”。 此時,我得到了文本字段的空指針異常(“ field”,在下面的代碼中)
public class MainActivity extends Activity {
public static final String PREFS_NAME = "MyPrefs";
// tip and tax rates
private String tax_rate;
private String tip_rate;
// default tip and tax
private String default_tax_rate = "10.0";
private String default_tip_rate = "12.0";
// tip and tax rate keys
public static final String TaxRate = "tax_rate_key";
public static final String TipRate = "tip_rate_key";
// dialog popup
final Context context = this;
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Retrieve preferences
sharedpreferences = getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
if (sharedpreferences.contains(TaxRate)) {
tax_rate = sharedpreferences.getString(TaxRate, "");
} else {
tax_rate = default_tax_rate;
}
if (sharedpreferences.contains(TipRate)) {
tip_rate = sharedpreferences.getString(TipRate, "");
} else {
tip_rate = default_tip_rate;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
// This method is called once the menu is selected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
switch (item.getItemId()) {
// If edit tip rate is selected
case R.id.item1:
View promptView1 = layoutInflater
.inflate(R.layout.prompt_tip, null);
editDialog(promptView1, TipRate, R.id.userInput_tip);
break;
// If edit tax rate is selected
case R.id.item2:
View promptView2 = layoutInflater
.inflate(R.layout.prompt_tax, null);
editDialog(promptView2, TaxRate, R.id.userInput_tax);
break;
}
return true;
}
private void editDialog(View promptView, final String key, int id) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to be the layout file of the alertdialog builder
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
final EditText field = (EditText) findViewById(id);
if (field.getText().toString().length() == 0) {
Toast.makeText(context,
"enter a valid number",
Toast.LENGTH_LONG).show();
}
else {
// Read the value inside the textfield as a
// string
SharedPreferences pref = getApplicationContext()
.getSharedPreferences(PREFS_NAME, 0);
// Initialize the sharedPreferences editor
Editor editor = pref.edit();
// get value in field
String value = field.getText().toString();
// pair the value in text field with the key
editor.putString(key, value);
editor.commit();
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alertD = alertDialogBuilder.create();
alertD.show();
}
}
更換:
final EditText field = (EditText) findViewById(id)
創建人:
final EditText field = (EditText) promptView.findViewById(id)
因為editText包含在hintView中,而不包含在“活動布局”中。
因為您的EditText的ID不是onClick方法中的參數,所以會出現異常。 變量id是單擊的按鈕的id。 您必須使用:
final EditText field = (EditText) findViewById(R.id.editTextId);
請參閱文檔 ,其中解釋了變量ID是什么。
在onclick()
使用以下代碼:
final EditText field = (EditText) promptView.findViewById(id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.