我创建了AlertDialog
:
public class ConfirmChoixDownloadDialogView extends AlertDialog {
private Activity activity; // this activity shows this Dialog
private View contenu, titleBar;
private final int OUI = DialogInterface.BUTTON_POSITIVE;
private final int NON = DialogInterface.BUTTON_NEGATIVE;
private final int NEUTRE = DialogInterface.BUTTON_NEUTRAL;
public static final int requestCode = 100;
private int btnClicked = NEUTRE;
private AsyncTask<String, Void, Void> indirectTask;
@SuppressLint("InlinedApi")
public ConfirmChoixDownloadDialogView(Context context, LayoutInflater inflater) {
super(context, AlertDialog.THEME_HOLO_DARK);
contenu = inflater.inflate(R.layout.msg_dialog, null);
((TextView)contenu.findViewById(R.id.msgText)).setText(context.getResources().getString(R.string.questionChargementWebVersMobile));
titleBar = inflater.inflate(R.layout.custom_dialog_title, null);
((ImageView)titleBar.findViewById(R.id.icone)).setImageDrawable(context.getResources().getDrawable(R.drawable.ic_action_about));
((TextView)titleBar.findViewById(R.id.titre)).setText(context.getResources().getString(R.string.titreConfirmMsgBox));
setCustomTitle(titleBar);
setView(contenu, 0, 0, 0, 0);
setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.button_oui), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
btnClicked = OUI;
dialog.dismiss();
}
});
setButton(DialogInterface.BUTTON_NEGATIVE, context.getResources().getString(R.string.button_non), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
btnClicked = NON;
dialog.dismiss();
}
});
setOnDismissListener(dismissListener);
}
public void afficher(Activity act, AsyncTask<String, Void, Void> tacheIndirecte) {
activity = act;
indirectTask = tacheIndirecte;
show();
}
private OnDismissListener dismissListener = new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (dialog instanceof ConfirmChoixDownloadDialogView) {
if (btnClicked == OUI) {
indirectTask.execute("commune.txt",
"parcelles.txt",
"rues.txt",
"batiments.txt",
"appartements.txt",
"occupants_appartement.txt",
"categories_activite.txt",
"natures_activite.txt",
"activites_dans_appart.txt",
"contribuables.txt");
}
else if (btnClicked == NON) {
Intent i = new Intent(activity, SelectionQuartierActivity.class);
activity.startActivity(i); // it makes app crash
}
}
}
};
}
正如您所看到的,我希望在点击activity
的{{1}}的情况下启动BUTTON_NEGATIVE
。但它崩溃了!那么如何在这种情况下启动Dialog
?
答案 0 :(得分:1)
showDialog(this, "", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(getApplicationContext(), NextActivty.class);
startActivity(i);
finish();
}
});
答案 1 :(得分:0)
在你的情况下,也许尝试使用AlertDialog的上下文而不是像这样调用它的Activity:
context.startActivity(i);
代替activity.startActivity(i)
答案 2 :(得分:0)
好的,我通过实施element.password = new Boolean(true)
的{{1}}方法并设置activity
,从activity
开始Dialog
的提示onWindowFocusChanged
的{{1}}内activity
的{{1}}变量!