动态设置自定义AlertDialog内容 [英] Dynamically set custom AlertDialog content

查看:226
本文介绍了动态设置自定义AlertDialog内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的设置功能自定义对话框:

I have a custom dialog that I set up as a function:

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

每当我试图用一个空指针异常动态设置XML领域的对话失败。我很为难,任何想法?我一定要添加一些清单自定义对话框?

The dialog fails whenever I try to set the XML fields dynamically with a Null Pointer Exception. I'm stumped, any ideas? Do I have to add something to the manifest for a custom dialog?

推荐答案

为此,首先:

查看布局= inflater.inflate(R.layout.popup,(ViewGroup中)findViewById(R.id.layout_root));

然后,定义在布局,做到这一点:

Then, after you define layout, do this:

ImageView的dialogimage =(ImageView的)layout.findViewById(R.id.popupimage);

呼叫 findViewByID()上创建新的布局,而不是在父内容视图。

Call findViewByID() on the new layout you created, not on the parent content view.

于是两个变化:订购和 layout.findView 不是 findView

So two changes: Ordering, and layout.findView not findView

这篇关于动态设置自定义AlertDialog内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆