开发者社区> 问答> 正文

关于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

展开
收起
爵霸 2016-03-18 11:04:30 2716 0
1 条回答
写回答
取消 提交回答
  • 如果换手机用安卓试一下程序,结果出现了一大片看不懂的报错。 看下有无出现关键报错信息: 1.Unknown failure (at android.os.Binder.execTransact(Binder.java:565)) 2.Error while Installing APKs 3.Session 'app': Error Installing APKs 4.Failed to finalize session : INSTALL_FAILED_INTERNAL_ERROR 根据以上信息百度并按照各路大神的解决办法尝试一遍,解决办法如下: 1.重新编译 2.手机拔掉,重新插 3.改变Android studio的设置,去掉第一个复选框的勾,但是这个方法对于我来说还是没有用,照样报错  首先,在虚拟机上运行了一遍程序,没有报错 其次,用以前的手机运行,也没有报错,说明是现在的手机有什么问题 然后,重启了一遍手机,进入开发者模式 这时候如果发现虽然USB调试都是打开状态 但是开发者模式处于关闭状态!!
    

    “答案来源于网络,供您参考” 希望以上信息可以帮到您!

    2019-09-27 20:38:07
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载