gpt4 book ai didi

android - alertDialog.getButton() 方法给出空指针异常android

转载 作者:IT王子 更新时间:2023-10-28 23:53:36 27 4
gpt4 key购买 nike

我打算用 layout_weight=1 创建 3 个按钮,对自定义对话框不感兴趣。所以我写了下面的代码。它不起作用。总是是按钮给我 null。这段代码有什么问题?

  AlertDialog dialog= new AlertDialog.Builder(this).create();
dialog.setIcon(R.drawable.alert_icon);
dialog.setTitle("title");
dialog.setMessage("Message");
dialog.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
}
});
Button yesButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE);
Log.w("Button",""+yesButton);//here getting null
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 1f);
yesButton.setLayoutParams(layoutParams);
dialog.show();

问候,Android 开发者。

最佳答案

在这里查看答案:http://code.google.com/p/android/issues/detail?id=6360

正如评论 #4 中所说,您必须先在对话框中调用 show(),然后才能访问按钮,它们事先不可用。有关如何在按钮准备好后立即修改按钮的自动解决方案,请参阅 Mickeys answer

关于android - alertDialog.getButton() 方法给出空指针异常android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4604025/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com