alertDialog.getButton()メソッドは、ヌル・ポインタ例外を与えるアンドロイド
-
25-09-2019 - |
質問
Iamはプレーニングカスタムdialog.So私に興味を持っていないlayout_weight = 1と3つのボタンを作成し得てcode.Itの下に書かれているがworking.Alwaysはいボタンは私には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://コード
答えをここで見てみましょう。 google.com/p/android/issues/detail?id=6360する
それはあなたがボタンにアクセスする前に、コメント#4で、あなたのダイアログでshow()
を呼び出す必要があります言うように、彼らは事前に利用できません。彼らは準備ができてご覧くださいすぐにミッキー答えの
などのボタンを変更する方法の自動解決のために他のヒント
これは私のために動作します:
AlertDialog alertDialog = new AlertDialog.Builder(this)
.setMessage(message)
.setCancelable(true)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do smthng
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do snthn
}
}).create();
alertDialog.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) { //
Button positiveButton = ((AlertDialog) dialog)
.getButton(AlertDialog.BUTTON_POSITIVE);
positiveButton.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.btn_default_holo_dark));
Button negativeButton = ((AlertDialog) dialog)
.getButton(AlertDialog.BUTTON_NEGATIVE);
positiveButton.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.btn_default_holo_dark));
}
});
alertDialog.show();
これだけ順に、コールalertDialog.setOnShowListener
create()
後
ありがとうwieux。しかし、新しい開発者が目的を理解するために私は再書き込み以下のコードを
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) {
}
});
dialog.show();
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);
所属していません StackOverflow