2 yangtao30122 yangtao30122 于 2016.01.13 13:38 提问

Android下面代码运行时候报错,仔细核对了控件名,没有问题,求各位大神帮忙看看

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

2个回答

lxk_1993
lxk_1993   Rxr 2016.01.13 14:00
已采纳

控件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);**

yangtao30122
yangtao30122 谢谢
接近 2 年之前 回复
Theresa_zxx
Theresa_zxx   2016.01.13 13:44

final EditText userName = (EditText) findViewById(R.id.edit_text_name);

这个要用填充的layout去find,改成:

final EditText userName = (EditText) view .findViewById(R.id.edit_text_name);

yangtao30122
yangtao30122 我点了采纳了,两个都点了,只会保留后面的吗
接近 2 年之前 回复
Theresa_zxx
Theresa_zxx 回复yangtao30122: 哎,都不采纳我的
接近 2 年之前 回复
yangtao30122
yangtao30122 感激涕零
接近 2 年之前 回复
Theresa_zxx
Theresa_zxx 这两句都要改
接近 2 年之前 回复
Theresa_zxx
Theresa_zxx final EditText userName = (EditText) findViewById(R.id.edit_text_name); final EditText passWord = (EditText) findViewById(R.id.edit_text_password);
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!