0

我在活动中显示一个对话框片段,尝试使用 show() 和 add() 方法。代码如下

HelpDialogFragment hdf = HelpDialogFragment.newInstance();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(hdf, "dialog");
ft.commit(); #crash here

我有时会从上面的代码中得到 IllegalStateException 错误,崩溃日志如下所示

java.lang.IllegalStateException: Activity has been destroyed
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1329)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:548)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:532)
at com.delight.activities.HomeActivity.showHelp(HomeActivity.java:311)

行号 ft.commit()上面代码中提到了HomeActivity.java 中的 311 。我的活动从兼容性库(最新更新)中扩展了 FragmentActivity。如果我使用show()方法,也会发生同样的崩溃。

HelpDialogFragment hdf = HelpDialogFragment.newInstance();
hdf.show(getSupportFragmentManager(), "dialog");  #same crash here

所以我得出了一个getSupportFragmentManager()导致我出现问题的结论,我不明白的是,我在代码的哪一部分使用了已被破坏的活动,据我所知,getSupportFragmentManager()应该把经理还给我存在的活动。

我需要一些帮助来解决这个问题。

4

3 回答 3

0

查看对话框片段你只需要调用 dialogfrag.show(Fmanager,tag); 通过事务添加它会产生异常

于 2012-07-23T12:49:23.207 回答
0

你的类应该扩展 FragmentActivity 而不是 Activity。这为我解决了问题

于 2013-04-15T14:10:46.750 回答
0

使用 getFragmentManager 而不是 getSupportFragmentManager

dialog.show(getFragmentManager(), "submitting"); // The second parameter doesn't really matter.

如果这不起作用,请扩展 DialogFragment 来执行此操作。关键是在构建对话框时使用您的 Activity 而不是使用支持库。

dialog = new AlertDialogFragment(title, message);
dialog.show(getFragmentManager(), "submitting");

警报对话框片段:

    public class AlertDialogFragment extends DialogFragment {

        static final String TAG_TITLE = "title";
        static final String TAG_MSG = "message";

        public AlertDialogFragment(){

        }

        public AlertDialogFragment(String title, String message) {
            Bundle b = new Bundle();
            b.putString(TAG_TITLE, title);
            b.putString(TAG_MSG, message);

            setArguments(b);
        }

        public AlertDialogFragment newInstance(String title, String message) {
            AlertDialogFragment frag = new AlertDialogFragment();
            Bundle b = new Bundle();
            b.putString(TAG_TITLE, title);
            b.putString(TAG_MSG, message);

            frag.setArguments(b);
            return frag;
        }

        public Dialog onCreateDialog(Bundle savedInstanceState) {
            String title = getArguments().getString(TAG_TITLE);
            String message = getArguments().getString(TAG_MSG);
                // Use getActivity()!
            return new AlertDialog.Builder(getActivity()).setTitle(title)
                    .setMessage(message)
                    .setNegativeButton("Dismiss", new OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dismiss();
                        }
                    })
                    .setOnCancelListener(new OnCancelListener() {

                        @Override
                        public void onCancel(DialogInterface dialog) {
                            Log.d(TAG, "Dialog cancel");
                        }
                    }).create();
        }

        @Override
        public void onCancel(DialogInterface dialog) {
            Log.d(TAG, "Cancel Dialog");
            super.onCancel(dialog);
        }

        @Override
        public void onDismiss(DialogInterface dialog) {
            Log.d(TAG, "Dismiss Dialog");
            super.onDismiss(dialog);
        }
    }

}
于 2012-07-23T13:00:28.390 回答