自定义对话框点击监听器android



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

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"?>

<TextView
    android:id="@+id/textViewAlertTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="3dp"
    android:paddingLeft="5dp"
    android:paddingTop="10dp"
    android:text="asdasdasdas"
    android:textColor="@android:color/holo_red_light" />
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/holo_red_light" />
<TextView
    android:id="@+id/textViewAlertMessage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="left|center"
    android:paddingBottom="3dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="10dp"
    android:text="asdasdasdaslasdjasldkasldlaskjdlaskjdlasskdjalskdjlsakdjlknxcnvksdfahflksaflkasjlakdlkasjd"
    android:textColor="@android:color/black" />
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
        android:id="@+id/buttonAlertCancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Cancel" />
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray" />
    <Button
        android:id="@+id/buttonAlertOk"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="OK" />
</LinearLayout>

它在活动中的实现

public class MainActivity extends AppCompatActivity implements CustomAlertDialog.OnButtonClick {
CustomAlertDialog customAlertDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_campaign_request);
    customAlertDialog=new CustomAlertDialog(MainActivity.this);
    customAlertDialog.setAlertTitle("xxxxx");
    customAlertDialog.setAlertMessage("ahdjashd");
    customAlertDialog.show();
}

@Override
public void onOkButtonClick() {
    Logger.e("click", "OK");
}
@Override
public void onCancelButtonClick() {
    Logger.e("click","CANCEL");
    customAlertDialog.dismiss();
}

}

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

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)

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

错误是因为您没有初始化CustomAlertDialog类中的onButtonClick。在CustomAlertDialog中添加此方法。

  public void setListener(CustomAlertDialog.OnButtonClic onButtonClick){
   this.onButtonClick = onButtonClick;
  }

从创建此对话框的Fragment中调用此方法。

customAlertDialog.setListener(this);

并检查CCD_ 5 中的CCD_

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

您没有初始化onButtonClick接口,所以当您尝试访问它时,它是空的,导致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;
     }
}

最新更新