自定义对话框的EditText返回null [英] Custom Dialog EditText returns null
本文介绍了自定义对话框的EditText返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
很多人都面临着类似的问题,我想我已经遵循固定在计算器上的所有帖子中提到的所有问题。
Many people have faced similar issues, and I think I have followed and fixed all the issues as mentioned in all those posts on stackoverflow.
- 的setContentView到我的布局
- 与dialog.findViewById初始化的EditText
但我仍然停留在空指针。我缺少什么?
But I am still stuck at the nullpointer. What am I missing ?
布局名称是serverchange.xml。 serverchange.xml的内容
Layout name is serverchange.xml. Contents of serverchange.xml are
<?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>
是由optionsmenu选择菜单上创建对话框。
Dialog is created on by selecting a menu on optionsmenu.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.serverChange:
showDialog(SERVER_CHANGE);
// newGame();
return true;
}
}
我得到一个NullPointerException异常在
I get a NullPointerException at
if (changeServerView == null) throw new NullPointerException() ;
code:
@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异常。
Here too I get a NullPointerException.
推荐答案
从XML文件错误ID refrences,
Wrong id refrences from xml file,
使用服务器
而不是 serverChange
changeServerView = (EditText) serverChangeDialog.findViewById(R.id.server);
^^^^^^
这篇关于自定义对话框的EditText返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文