我有一个自定义对话框,我将其设置为一个函数:
public void customDialog(Bitmap bm, String title){
TextView dialogtext = (TextView)findViewById(R.id.layout_root);
//For above also tried r.id.popup which is the containing file for the layout
ImageView dialogimage = (ImageView)findViewById(R.id.popupimage);
dialogtext.setText(title);
dialogimage.setImageBitmap(bm);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.layout_root));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.show();
}
每当我尝试使用Null指针异常动态设置XML字段时,对话框都会失败.我很难过,有什么想法吗?我是否必须在清单中添加一些自定义对话框?
最佳答案 首先这样做:
View layout = inflater.inflate(R.layout.popup,(ViewGroup)findViewById(R.id.layout_root));
然后,在定义布局后,执行以下操作:
ImageView dialogimage =(ImageView)layout.findViewById(R.id.popupimage);
在您创建的新布局上调用findViewByID(),而不是在父内容视图上调用.
所以有两个变化:Ordering和layout.findView没有findView