button_user_alert.setOnClickListener(new OnClickListener() {
View view = View.inflate(DialogComponentTest.this, R.layout.user_dialog, null);
final EditText userName = (EditText) findViewById(R.id.edit_text_name);
final EditText passWord = (EditText) findViewById(R.id.edit_text_password);
@Override
public void onClick(View v) {
new AlertDialog.Builder(DialogComponentTest.this)
.setView(view)
.setPositiveButton("取消", null)
.setNegativeButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//這裡出現nullPointerException,不知道該怎麼處理,找不出問題所在
String strUserName = userName.getText().toString();
String strPassWord = passWord.getText().toString();
Toast.makeText(DialogComponentTest.this,strUserName +" : "+strPassWord, 0).show();
}
})
.show();
}
});
錯誤信息如下:
01-13 13:41:16.472: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
01-13 13:41:20.792: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
01-13 13:41:27.512: E/AndroidRuntime(1878): FATAL EXCEPTION: main
01-13 13:41:27.512: E/AndroidRuntime(1878): java.lang.NullPointerException
01-13 13:41:27.512: E/AndroidRuntime(1878): at com.example.uitest.DialogComponentTest$3$1.onClick(DialogComponentTest.java:97)
01-13 13:41:27.512: E/AndroidRuntime(1878): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
01-13 13:41:27.512: E/AndroidRuntime(1878): at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 13:41:27.512: E/AndroidRuntime(1878): at android.os.Looper.loop(Looper.java:137)
01-13 13:41:27.512: E/AndroidRuntime(1878): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-13 13:41:27.512: E/AndroidRuntime(1878): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 13:41:27.512: E/AndroidRuntime(1878): at java.lang.reflect.Method.invoke(Method.java:511)
01-13 13:41:27.512: E/AndroidRuntime(1878): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-13 13:41:27.512: E/AndroidRuntime(1878): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-13 13:41:27.512: E/AndroidRuntime(1878): at dalvik.system.NativeStart.main(Native Method)
01-13 13:41:27.572: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
01-13 13:41:29.352: E/Trace(1899): error opening trace file: No such file or directory (2)
01-13 13:41:29.383: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
01-13 13:41:29.393: E/SurfaceFlinger(794): ro.sf.lcd_density must be defined as a build property
控件android
button_user_alert.setOnClickListener(new OnClickListener() {
View view = View.inflate(DialogComponentTest.this, R.layout.user_dialog, null);**
final EditText userName = (EditText) view.findViewById(R.id.edit_text_name);
final EditText passWord = (EditText) view.findViewById(R.id.edit_text_password);**