我制作了一个自定义提醒对话框:
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final FrameLayout frameView = new FrameLayout(this);
builder.setView(frameView);
final AlertDialog alertDialog = builder.create();
LayoutInflater inflater = alertDialog.getLayoutInflater();
dialoglayout = inflater.inflate(R.layout.customized_builder, frameView);
alertDialog.show();
ButtonFlat acceptButton = (ButtonFlat) findViewById(R.id.acceptButtonDialog);
acceptButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
ButtonFlat declineButton = (ButtonFlat) findViewById(R.id.cancelButtonDialog);
declineButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
问题是,当我单击按钮显示此警告对话框时,它会在此行生成空指针异常:
acceptButton.setOnClickListener(new View.OnClickListener()
答案 0 :(得分:1)
似乎在运行时,操作系统不知道这个按钮在哪里。 试试这个:
ButtonFlat acceptButton = (ButtonFlat) dialoglayout.findViewById(R.id.acceptButtonDialog);
acceptButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
ButtonFlat declineButton = (ButtonFlat) dialoglayout.findViewById(R.id.cancelButtonDialog);
declineButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
just add dialoglayout.findViewById()