【发布时间】:2018-11-21 07:59:41
【问题描述】:
在带有自定义视图的自定义对话框类中,我想处理来自 Activity 或片段的按钮点击,我创建了一个接口来处理按钮点击但显示错误。
尝试在空对象引用上调用接口方法“void com.ymcaspi.util.CustomDialog$DialogInterface.doLogin(com.ymcaspi.util.CustomDialog)”
我的对话框类是
public class CustomDialog extends Dialog implements
android.view.View.OnClickListener {
public Activity activity;
public Button btnYes, btnNo;
CustomDialog customDialog;
public CustomDialog(Activity activity) {
super(activity);
this.activity = activity;
}
DialogInterface dialogInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.alert_login);
btnYes = (Button) findViewById(R.id.btn_yes);
btnNo = (Button) findViewById(R.id.btn_no);
btnYes.setOnClickListener(this);
btnNo.setOnClickListener(this);
}
//in custom adapter class i want to handle click of button from Activity or fragment, I have created a interface for handling button click
//but showing
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_yes:
customDialog=new CustomDialog(activity);
dialogInterface.doLogin(customDialog);
break;
case R.id.btn_no:
dismiss();
break;
default:
break;
}
dismiss();
}
public interface DialogInterface{
void doLogin(CustomDialog dialog);
}
}
我已经在fragment中实现了这个接口但是不工作?
【问题讨论】:
-
显然,您的
dialogInterface从未被分配,您打算如何处理它? -
dialogInterface 为空,你忘记初始化了。
-
@Aaron 我想在我的任务完成后关闭活动对话框。
-
@farhana 在这种情况下,您可以在同一个类中创建一个方法
doLogin而无需接口。