如何从负按钮alertdialog调用片段活动?

时间:2014-09-17 08:13:00

标签: android android-fragments android-alertdialog

ExamplesActivity.class是片段活动

LoginActivity.class是活动

在活动中运作良好,但片段活动停止工作? 请帮忙 。

  new AlertDialog.Builder(getActivity())
    .setTitle("Logout Warning !!!")
    .setMessage("Apakah anda yakin keluar dari program ini?")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            continue with delete
            Intent intent = new Intent(((Dialog) dialog).getContext(), LoginActivity.class);
            startActivityForResult(intent, 0);
        }
     })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 

            Intent intent= new Intent(((Dialog) dialog).getContext() ,ExamplesActivity.class);
            startActivityForResult(intent, 0); 


        }
     })
    .setIcon(android.R.drawable.ic_dialog_alert)
     .show();

1 个答案:

答案 0 :(得分:0)

我不明白你为什么在得到Dialog context时试图获得FragmentActivity。 请尝试以下代码,如果失败,请发布您的logcat。

new AlertDialog.Builder(getActivity())
    .setTitle("Logout Warning !!!")
    .setMessage("Apakah anda yakin keluar dari program ini?")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which)
        { 
            continue with delete
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivityForResult(intent, 0);
        }
    })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
            Intent intent= new Intent((getActivity(), ExamplesActivity.class);
            startActivityForResult(intent, 0); 
        }
    })
    .setIcon(android.R.drawable.ic_dialog_alert)
    .show();