我在setPositiveButton onClick()方法的EditText()上使用getText()方法,当我执行我的操作时,我有一个RuntimeException。这是代码:
protected Dialog onCreateDialog(int id) {
switch(id) {
case NICKNAME_DIALOG_ID:
LayoutInflater nicknameInflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View nicknameLayout = nicknameInflater.inflate
(R.layout.nickname_dialog, (ViewGroup) findViewById
(R.id.linearLayout_root));
final EditText nickname = (EditText)findViewById(R.id.editText_nickname);
AlertDialog.Builder nicknameBuilder = new AlertDialog.Builder(this);
nicknameBuilder.setView(nicknameLayout);
nicknameBuilder.setNegativeButton
(R.string.nickname_dialog_cancel, new DialogInterface.OnClickListener()
{
@SuppressWarnings("deprecation")
public void onClick(DialogInterface dialog,
int whichButton) {
SettingsActivity.this.removeDialog(NICKNAME_DIALOG_ID);
}
});
nicknameBuilder.setPositiveButton(R.string.nickname_dialog_ok,
new DialogInterface.OnClickListener() {
@SuppressWarnings("deprecation")
public void onClick(DialogInterface dialog,
int which) {
Editor editor = m_GameSettings.edit();
editor.putString
(VolimHrvatsku.GAME_PREFERENCES_NICKNAME,
nickname.getText().toString());//here's the error
editor.commit();
SettingsActivity.this.removeDialog(NICKNAME_DIALOG_ID);
}
});
AlertDialog nicknameDialog = nicknameBuilder.create();
return nicknameDialog;
我曾经遇到过与button.getText()方法类似的问题,它显示了一些int值,就像这个。
我仍然遇到getText()问题。这是例外:
!SettingsActivity$4.onClick(DialogInterface, int) line: 110
AlertDialog(AlertController$ButtonHandler).handleMessage(Message) line: 158
AlertController$ButtonHandler(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4633
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line:
not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 858
ZygoteInit.main(String[]) line: 616
NativeStart.main(String[]) line: not available [native method]
答案 0 :(得分:3)
我认为这是一个Nullpointer Exception,因为他的View通胀似乎已经消失了。
尝试使用此行代替您的
final EditText nickname = (EditText)nicknameLayout.findViewById(R.id.editText_nickname);