0
很多人都遇到过类似的问题,我想我已经跟上并解决了所有在stackoverflow上所有这些帖子中提到的问题。自定义对话框EditText返回null
- 的setContentView到我的布局
- 初始化的EditText与dialog.findViewById
但我仍然停留在空指针。我错过了什么?
布局名称是serverchange.xml。 serverchange.xml的内容是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/server"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textUri" />
<TextView
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:editable="true" />
</LinearLayout>
通过在选项菜单上选择菜单创建对话框。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.serverChange:
showDialog(SERVER_CHANGE);
// newGame();
return true;
}
}
我在
if (changeServerView == null) throw new NullPointerException() ;
代码得到一个NullPointerException:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case SERVER_CHANGE:
serverChangeDialog = new Dialog(this);
serverChangeDialog.setContentView(R.layout.serverchange);
serverChangeDialog.setTitle("Change Server");
serverChangeDialog.setOnKeyListener(this);
serverChangeDialog.show();
changeServerView = (EditText) serverChangeDialog.findViewById(R.id.serverChange);
status = (TextView) serverChangeDialog.findViewById(R.id.status);
if (changeServerView == null) throw new NullPointerException() ;
}
return null;
}
我的安其实施
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (dialog == serverChangeDialog) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
String backupServer = server;
server = changeServerView.getText().toString();
这里也是我得到一个NullPointerException。从XML文件