我使用NullPointerException
中的EditText
获得了DialogAlert
。我的代码如下所示:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WelcomeDialog();
}
public Dialog WelcomeDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Server");
builder.setMessage("Enter ADDRESS and PORT:");
// Get the layout inflater
LayoutInflater inflater = this.getLayoutInflater();
// Pass null as the parent view because its going in the dialog layout
View v = inflater.inflate(R.layout.dialog_welcome, null);
builder.setView(v);
// Use an EditText view to get user input.
final EditText input1 = new EditText(this);
input1.setId(address);
builder.setView(input1);
final EditText ETaddress = (EditText) v.findViewById(address);
final EditText input2 = new EditText(this);
input2.setId(port);
builder.setView(input2);
final EditText ETport = (EditText) v.findViewById(port);
builder.setView(v)
// Inflate and set the layout for the dialog
// Add action buttons
.setPositiveButton("connect", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// The NULLPOINTER is in this line:
String str1 = ETaddress.getText().toString();
((VariableHolder) getApplication()).setADDRESS(str1);
int str2 = Integer.parseInt(ETport.getText().toString());
((VariableHolder) getApplication()).setPORT(str2);
set = true;
}
});
builder.create().show();
return builder.create();
}
}
}
我怀疑问题在这里(错误的声明或类似的东西):
final EditText input1 = new EditText(this);
input1.setId(address);
builder.setView(input1);
final EditText ETaddress = (EditText) v.findViewById(address);
...但我无法弄明白,这是错误的。有什么想法吗?
修改
这是logcat输出:
> 01-06 19:40:28.241 4694-4694/com.example.console2
> E/AndroidRuntime﹕ FATAL EXCEPTION: main
> java.lang.NullPointerException
> at com.example.MainActivity$2.onClick(MainActivity.java:88)
> at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
> at android.os.Handler.dispatchMessage(Handler.java:99)
> at android.os.Looper.loop(Looper.java:137)
> at android.app.ActivityThread.main(ActivityThread.java:4611)
> at java.lang.reflect.Method.invokeNative(Native Method)
> at java.lang.reflect.Method.invoke(Method.java:511)
> at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
> at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
好吧,这是一个愚蠢的错误。只能在地址前忘记R.id
端口:
final EditText ETaddress = (EditText)v.findViewById(R.id.address); // R.id. !!!
答案 1 :(得分:1)
该v视图不包含您的edittext,因此 v.findViewById(地址)将仅返回null。
试试这个,
http://www.mkyong.com/android/android-prompt-user-input-dialog-example/
答案 2 :(得分:1)
如果你怀疑那个问题,那就做下一个!
1删除所有这些的最终修饰符!
2在setContentView(R.layout.activity_main);
之后找到他们的观点
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText input1 = new (EditText)findViewById(R.id.yourEditTextId);
EditText ETaddress = (EditText)findViewById(R.id.yourEditTextId);
然后做其余的事情!