Iam刨給創建3個按鈕與layout_weight = 1,不感興趣的自定義dialog.So我寫下面的code.It不工作。總是是按鈕給我null 。 這段代碼有什麼問題?alertDialog.getButton()方法給出空指針異常android
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開發人員。
不過問題persist.No使用呼叫
alertDialog.setOnShowListener
。 – ADIT 2011-01-05 13:09:53請閱讀註釋#4,使用dialog.show();之前使用getButton() – vieux 2011-01-05 13:18:31
謝謝wieux.It工作 – ADIT 2011-01-05 13:46:38