Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自定义对话框单击监听器android?_Android_Interface_Dialog - Fatal编程技术网

自定义对话框单击监听器android?

自定义对话框单击监听器android?,android,interface,dialog,Android,Interface,Dialog,我创建了一个扩展dialog类的自定义对话框 public class CustomAlertDialog extends Dialog implements View.OnClickListener { private TextView tvAlertTitle, tvAlertMessage; private Button butAlertOk, butAlertCancel; private CustomAlertDialog.OnButtonClick onButtonClick;

我创建了一个扩展dialog类的自定义对话框

public class CustomAlertDialog extends Dialog implements View.OnClickListener {

private TextView tvAlertTitle, tvAlertMessage;
private Button butAlertOk, butAlertCancel;
private CustomAlertDialog.OnButtonClick onButtonClick;

public CustomAlertDialog(Context context) {
    super(context);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.setContentView(R.layout.layout_alert_dialog);
    initViews();
}

//initialise views
private void initViews() {
    tvAlertTitle = (TextView) findViewById(R.id.textViewAlertTitle);
    tvAlertMessage = (TextView) findViewById(R.id.textViewAlertMessage);
    butAlertOk = (Button) findViewById(R.id.buttonAlertOk);
    butAlertCancel = (Button) findViewById(R.id.buttonAlertCancel);
    butAlertOk.setOnClickListener(this);
    butAlertCancel.setOnClickListener(this);
}

//set title for dialog
public void setAlertTitle(String title) {
    tvAlertTitle.setText(title);
}

//set message for dialog
public void setAlertMessage(String message) {
    tvAlertMessage.setText(message);
}



@Override
public void onClick(View v) {
    if (v == butAlertOk) {
        onButtonClick.onOkButtonClick();
    }
    if (v == butAlertCancel) {
        onButtonClick.onCancelButtonClick();
    }
}

public interface OnButtonClick {
    void onOkButtonClick();

    void onCancelButtonClick();
}
}

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
}

当我点击任何一个按钮时,我得到这个异常

java.lang.NullPointerException
        at com.vfirst.ui.CustomAlertDialog.onClick(CustomAlertDialog.java:57)
        at android.view.View.performClick(View.java:4575)
        at android.view.View$PerformClick.run(View.java:18578)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5127)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
        at dalvik.system.NativeStart.main(Native Method)

代码有什么问题?我是否正确实现了该接口?

您没有初始化
onbutton单击
接口,因此当您尝试访问该接口时,该接口为空,从而导致
NullPointerException
。您可以这样做

public CustomAlertDialog(Context context) {
    super(context);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.setContentView(R.layout.layout_alert_dialog);
    initViews();

    if (context instanceof CustomAlertDialog.OnButtonClick) {
       onButtonClick = (CustomAlertDialog.OnButtonClick)context;
     }
}

错误是因为您尚未初始化
on按钮单击
CustomAlertDialog
类中的
。在
CustomAlertDialog
中添加此方法

  public void setListener(CustomAlertDialog.OnButtonClic onButtonClick){
   this.onButtonClick = onButtonClick;
  }
从创建此对话框的片段中调用此方法

customAlertDialog.setListener(this);
并检查
onButtonClick
onClick

public void onClick(View v) {
    if(onButtonClick !=null){
      if (v == butAlertOk) {
            onButtonClick.onOkButtonClick();
        }
        if (v == butAlertCancel) {
            onButtonClick.onCancelButtonClick();
        }
     }
   }

您忘记初始化onButtonClick。谢谢,这只是问题所在。现在,当我在片段中使用它时,我发现这个错误HomeActivity不能转换为CustomAlertDialog$OnButtonClickI,我将它初始化为CustomAlertDialog=new CustomAlertDialog(getActivity()),谢谢,这只是个问题。现在,当我在片段中使用它时,我收到以下错误:HomeActivity不能强制转换到CustomAlertDialog$OnButton单击…我将其初始化为CustomAlertDialog=new CustomAlertDialog(getActivity())@WISHY更新了我的答案,请查看
customAlertDialog.setListener(this);
public void onClick(View v) {
    if(onButtonClick !=null){
      if (v == butAlertOk) {
            onButtonClick.onOkButtonClick();
        }
        if (v == butAlertCancel) {
            onButtonClick.onCancelButtonClick();
        }
     }
   }