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();