Alert对话框调用中的活动已被销毁

时间:2017-03-10 08:34:42

标签: java android android-fragments alertdialog

我一直在尝试在我的应用中添加自定义提醒对话框,但在调用dialog.show方法时出现异常

Activity has been destroyed
                  at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1458)
                  at android.app.BackStackRecord.commitInternal(BackStackRecord.java:707)
                  at android.app.BackStackRecord.commit(BackStackRecord.java:671)
                  at android.app.DialogFragment.show(DialogFragment.java:230)
                  at com.kdpl.motodriver.SelectBike$subSelect.showNoticeDialog(SelectBike.java:144)
                  at com.kdpl.motodriver.SelectBike$3.onItemLongClick(SelectBike.java:83)
                  at android.widget.AbsListView.performLongPress(AbsListView.java:3262)
                  at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3179)
                  at android.os.Handler.handleCallback(Handler.java:751)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6123)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

警报对话类

public class PopUpAlert extends DialogFragment {
public interface NoticeDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);
}
NoticeDialogListener mListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.popup_util, null))
            // Add action buttons
            .setPositiveButton(R.string.btn_ok_text, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // sign in the user ...
                }
            })
            .setNegativeButton(R.string.btn_cancel_text, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    getDialog().cancel();
                }
            });
    return builder.create();
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        Activity a;
        if(context instanceof Activity){
            a = (Activity)context;
        }
        else{
            a = new Activity();
        }
        // Instantiate the NoticeDialogListener so we can send events to the host
        mListener = (NoticeDialogListener) a;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(context.toString()
                + " must implement NoticeDialogListener");
    }
}

}

班级通话提醒对话

public class MyClass extends AppCompatActivity implements SearchView.OnQueryTextListener {
lst = (ListView)findViewById(R.id.lst );
lst.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
            new subClass().showNoticeDialog(); // Calling Dialog               
            return true;
        }
    });

 ......

 private class subClass extends FragmentActivity  implements PopUpAlert.NoticeDialogListener{
    public void showNoticeDialog() {            
        DialogFragment dialog = new PopUpAlert();
        dialog.show(getFragmentManager(), "NoticeDialogFragment"); // Getting exception here
    }


    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {
        // User touched the dialog's positive button

    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        // User touched the dialog's negative button

    }
 }
}
}

1 个答案:

答案 0 :(得分:0)

尝试使用EventBus从DialogFragment中调用活动而不是使用直接链接来发布事件。如果您使用来自DialogFragment的活动链接,请使用WeakReference&lt;&gt;避免内存泄漏