我的代码是这样的:我点击选项菜单,选择"编辑提示率"。然后,会出现一个弹出窗口,其中包含文本字段,取消按钮和保存按钮。我在文本字段中输入一个值,然后单击“保存”。此时我得到文本字段的空指针异常("字段",在下面的代码中)
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();
}
}
答案 0 :(得分:0)
替换:
final EditText field = (EditText) findViewById(id)
by:
final EditText field = (EditText) promptView.findViewById(id)
因为editText包含在promptView中而不包含在Activity Layout中。
答案 1 :(得分:0)
您得到一个例外,因为您的EditText的id不是onClick方法中的参数。 变量id是单击的按钮的id。你必须使用:
final EditText field = (EditText) findViewById(R.id.editTextId);
请参阅documentation,解释变量id是什么。
答案 2 :(得分:0)
onclick()
中的使用此:
final EditText field = (EditText) promptView.findViewById(id)