这是一个showDialog,Button的点击事件,之前的代码一直出空指针的错误。现在找到了原因了。
代码中红色标记的layout,之前一直用的是this(我是新手,习惯了)一直出空指针的错误。原来要用view对象来调用findViewById方法。
private void showCustomeDialog() {
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = MainActivity.this;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.customer_layout, null);
TextView textView = (TextView) layout.findViewById(R.id.tv01);
ImageView imageView = (ImageView) layout.findViewById(R.id.iv01);
textView.setText("Hello, Welcome to Mr Wei's blog!");
imageView.setImageResource(R.drawable.ic_launcher);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();}
layoutinflater 空指针错误
最新推荐文章于 2021-05-27 18:04:29 发布