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文件