Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建对话框在Android中显示NullPointerException_Java_Android - Fatal编程技术网

Java 创建对话框在Android中显示NullPointerException

Java 创建对话框在Android中显示NullPointerException,java,android,Java,Android,这是我的模型课 public class VersionEntity { private int versionCode; private String path; private String desc; public int getVersionCode() { return versionCode; } public void setVersionCode(int versionCode) { this.ve

这是我的模型课

public class VersionEntity {
    private int versionCode;
    private String path;
    private String desc;

    public int getVersionCode() {
        return versionCode;
    }
    public void setVersionCode(int versionCode) {
        this.versionCode = versionCode;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }


}
这是我的密码

protected void showDialog(final Message msg) {
    //LINE A
    String path = ((VersionEntity)(msg.obj)).getPath();

    Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("new apk");
    builder.setMessage(((VersionEntity)(msg.obj)).getDesc());
    builder.setCancelable(false);
    builder.setPositiveButton("OK", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            //LINE B
//          String path = ((VersionEntity)(msg.obj)).getPath();

        }
    });

    builder.setNegativeButton("Cancle", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Message msg = handler.obtainMessage(2);
            handler.sendMessage(msg);
        }
    });

    builder.show();
}
当新的apk出现时,我创建了一个对话框

如果我取A行并删除B行,就可以了

如果我取B行并删除A行,然后按“确定”按钮,它会显示
NullPointerException
,我不知道发生了什么。如何修复它

2017-01-05添加内容


我将函数
protectedvoidshowDialog(final Message msg)
更改为
protectedvoidshowDialog(final VersionEntity ve)
,则
nullpointerexception
消失,但我仍然不知道为什么?

无法显示
生成器。您可以显示一个
对话框

AlertDialog dialog = builder.create() ;
dialog.show() ;

你可以看看代码。我想你可以试试这个。希望这会有帮助

请出示您的日志。保留A行和删除B行有什么问题?您已经有了变量
路径
。您没有创建生成器吗?我认为你应该写
builder.create().show()
我将函数
protected void showDialog(final Message msg)
更改为
protected void showDialog(final VersionEntity ve)
,则
nullpointerexception
消失,但我仍然不知道为什么?我已经尝试过了,但仍然没有使用。在我的路上,对话框仍然出现
 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx);

                                    alertDialogBuilder.setTitle("Title");

                                    alertDialogBuilder
                                            .setMessage("Message")
                                            .setCancelable(false)
                                            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                                    public void onClick(DialogInterface dialog, int id)
                                                    {
                                                            //Do something
                                                    }
                                            })
                                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                                    public void onClick(DialogInterface dialog, int id) {

                                                            //Do something

                                                    }
                                            });

                                    AlertDialog alertDialog = alertDialogBuilder.create();

                                    alertDialog.show();