[英]Custom method for AlertDialog throws “Attempt to invoke … on a null object reference” Android
I've created a custom AlertDialog
which has an ImageView
, 3 TextView
and a Button to dismiss the dialog. 我创建了一个自定义
AlertDialog
,它具有一个ImageView
,3个TextView
和一个用于关闭对话框的按钮。 I need to set the ImageView
and the 3 TextView
's text from my Activity
, So inside the custom AlertDialog
class I've created the following method: 我需要从
Activity
设置ImageView
和3 TextView
的文本,因此在自定义AlertDialog
类中,我创建了以下方法:
public class ProductDialog extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button close;
public ImageView pImage;
public TextView title;
public TextView utensils;
public TextView preparation;
public ProductDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.product_dialog);
close = (Button) findViewById(R.id.popClose);
close.setOnClickListener(this);
pImage = (ImageView) findViewById(R.id.popImg);
title = (TextView) findViewById(R.id.popTitle);
utensils = (TextView) findViewById(R.id.popUtensili);
preparation = (TextView) findViewById(R.id.popPreparazione);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.popClose:
dismiss();
break;
default:
break;
}
dismiss();
}
public void setImage(String urlImg) {
Picasso.with(getContext())
.load(urlImg)
.placeholder(R.drawable.placeholder)
//.transform(new RoundedTransformation(10, 0))
.fit().centerCrop()
.into(pImage);
}
public void setTitle(String txtTitle) {
title.setText(txtTitle);
}
public void setUtensils(String txtUtensils) {
utensils.setText(txtUtensils);
}
public void setPreparation(String txtPreparation) {
preparation.setText(txtPreparation);
}
}
And I call them from my Activity
like this: 我从我的
Activity
这样称呼他们:
pDetails = new ProductDialog(MyActivity.this);
pDetails.setTitle("asd");
pDetails.setUtensils("asd");
pDetails.setPreparation("asd");
pDetails.setImage(imageUrl);
pDetails.show();
But as soon as setTitle()
gets fired I get the following error: 但是,一旦
setTitle()
被解雇,我就会收到以下错误:
07-23 13:03:10.810: E/AndroidRuntime(3117): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test/com.test.test.MyActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
07-23 13:03:10.810:E / AndroidRuntime(3117):java.lang.RuntimeException:无法启动活动ComponentInfo {com.test.test / com.test.test.MyActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'
What am I missing in order to make it work? 为了使它有效,我缺少什么?
You are calling those setters when views are not initialized. 当视图未初始化时,您正在调用这些设置器。 onCreate() method is called when
show()
method is called. 调用
show()
方法时将调用onCreate()方法。 but these setters are being called before show method. 但是这些设置方法在show方法之前被调用。 that is causing that error.
导致该错误。
If you want to set those values from mainActivity you have to make interface to get those values or send data in constructor. 如果要从mainActivity设置这些值,则必须创建接口以获取这些值或在构造函数中发送数据。
I have modified you code that can explain you how to achieve that you want. 我修改了您的代码,可以解释您如何实现所需的代码。 there are plenty of ways, here is the simplest one:
有很多方法,这是最简单的方法:
public Activity c;
public Dialog d;
public Button close;
public ImageView pImage;
public TextView title;
public TextView utensils;
public TextView preparation;
// variables those will handle values. i did not implement it for image you can do same for that also.
private String titleValue;
private String utensilsValue;
private String preparationValue;
public ProductDialog(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.product_dialog);
close = (Button) findViewById(R.id.popClose);
close.setOnClickListener(this);
pImage = (ImageView) findViewById(R.id.popImg);
title = (TextView) findViewById(R.id.popTitle);
utensils = (TextView) findViewById(R.id.popUtensili);
preparation = (TextView) findViewById(R.id.popPreparazione);
title.setText(titleValue);
utensils.setText(utensilsValue);
preparation.setText(preparationValue);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.popClose:
dismiss();
break;
default:
break;
}
dismiss();
}
public void setImage(String urlImg) {
Picasso.with(getContext())
.load(urlImg)
.placeholder(R.drawable.placeholder)
//.transform(new RoundedTransformation(10, 0))
.fit().centerCrop()
.into(pImage);
}
// Created getter and setter to get get value form mainActivity.
public void setTitle(String txtTitle) {
titleValue = (txtTitle);
}
public void setUtensils(String txtUtensils) {
utensilsValue = (txtUtensils);
}
public void setPreparation(String txtPreparation) {
preparationValue = (txtPreparation);
}
}
This is because your dialog is not created. 这是因为未创建对话框。 Use builder pattern to save the values before creating.
在创建之前,使用构建器模式保存值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.