Android 使用片段或活动的自定义视图处理自定义对话框类的单击
在具有自定义视图的自定义对话框类中,我希望处理来自活动或片段的按钮单击,我已经创建了一个用于处理按钮单击但显示错误的界面 尝试在空对象引用上调用接口方法“void com.ymcaspi.util.CustomDialog$DialogInterface.doLogin(com.ymcaspi.util.CustomDialog)” 我的对话课是Android 使用片段或活动的自定义视图处理自定义对话框类的单击,android,interface,dialog,Android,Interface,Dialog,在具有自定义视图的自定义对话框类中,我希望处理来自活动或片段的按钮单击,我已经创建了一个用于处理按钮单击但显示错误的界面 尝试在空对象引用上调用接口方法“void com.ymcaspi.util.CustomDialog$DialogInterface.doLogin(com.ymcaspi.util.CustomDialog)” 我的对话课是 public class CustomDialog extends Dialog implements android.view.View.O
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);
}
}
我已经在片段中实现了此接口,但没有工作?您没有在对话框中初始化对话框接口,如果您已经在活动中实现了此接口,请将活动设置为对话框接口
public CustomDialog(Activity activity,DialogInterface dialogInterface ) {
super(activity);
this.activity = activity;
this.dialogInterface = dialogInterface ;
}
您没有在对话框上初始化对话框界面,如果您在活动上实现了该界面,请将活动设置为对话框界面
public CustomDialog(Activity activity,DialogInterface dialogInterface ) {
super(activity);
this.activity = activity;
this.dialogInterface = dialogInterface ;
}
以下是一个示例,如果您打算从对话框接收有关活动的回调,可以尝试:
class YourActivity extends Activity implements DialogInterface {
void showDialog() {
CustomDialog dialog = // init your CustomDialog
dialog.setOnLoginClickListener(this);
dialog.show();
}
void doLogin() {
// Button yes has been clicked, do stuff...
}
}
并创建一个方法,在CustomDialog
类中分配侦听器:
public class CustomDialog extends Dialog implements OnClickListener {
private DialogInterface dialogInterface;
public void setOnLoginClickListener(DialogInterface dialogInterface) {
this.dialogInterface = dialogInterface;
}
}
以下是一个示例,如果您打算从对话框接收有关活动的回调,可以尝试:
class YourActivity extends Activity implements DialogInterface {
void showDialog() {
CustomDialog dialog = // init your CustomDialog
dialog.setOnLoginClickListener(this);
dialog.show();
}
void doLogin() {
// Button yes has been clicked, do stuff...
}
}
并创建一个方法,在CustomDialog
类中分配侦听器:
public class CustomDialog extends Dialog implements OnClickListener {
private DialogInterface dialogInterface;
public void setOnLoginClickListener(DialogInterface dialogInterface) {
this.dialogInterface = dialogInterface;
}
}
显然,您的
dialogInterface
从未分配,您打算如何处理它?dialogInterface的可能副本为空,您忘记了初始化它。@Aaron我想在任务完成后从活动中关闭对话框。@farhana在这种情况下,您可以在没有接口的情况下在同一类中创建方法doLogin
。显然,您的dialogInterface
从未分配过,您打算如何处理它?dialogInterface的可能副本为null,您忘了初始化它。@Aaron我想在任务完成后从“活动”关闭对话框。@farhana在这种情况下,您可以在同一个类中创建一个方法doLogin
,而无需接口。。。。或将对话框接口的实例传递给构造函数或setter@AndreyBusik可以正确解释。我想在响应要关闭对话框后调用doLogin
中的服务。@farhana:您的活动中有该对话框的实例,因此在收到响应后,您可以检查对话框是否显示,那么请忽略它。@ArashGM我这样做了,显示了不兼容的类型,并且我也在fragment和activity中实现了接口。。。。或将对话框接口的实例传递给构造函数或setter@AndreyBusik可以正确解释。我想在响应要关闭对话框后调用doLogin
中的服务。@farhana:您的活动中有该对话框的实例,因此在收到响应后,您可以检查对话框是否显示,然后忽略它。@ArashGM我这样做了,显示了不兼容的类型,我也在fragment和activity中实现了接口。我从您的代码中得到了这个概念,我做了一个小小的更改,您的setOnLoginClickListener
给了我对话框引用。所以我在构造函数中传递了接口的引用。实际上,我正在将活动
与接口进行比较,但我感到困惑。我从您的代码中得到了概念,我做了一个小的更改,您的设置了loginclicklistener
给了我对话框引用。因此,我在构造函数中传递了接口的引用。实际上,我是在将活动
与界面进行比较,但我感到困惑。