对话框中的 AutocompleteSearchFragment 给出 InflateException

AutocompleteSearchFragment in Dialog gives InflateException

我在 Dialog 中使用 Google 地图的地点 API 的 AutoCompleteSearchFragment。当我启动对话框、关闭它然后重新启动它时,会出现我遇到的错误。

错误信息:

Error inflating class fragment. Caused by: java.lang.IllegalArgumentException: Binary XML file line #69: Duplicate id 0x7f0a0027, tag null, or parent id 0x7f0a00c7 with another fragment for `com.google.android.libraries.places.widget.AutocompleteSearchFragmet

我的代码:

Dialog alert = new Dialog(MainActivity.this);
alert.requestWindowFeature(Window.FEATURE_NO_TITLE);
alert.setContentView(R.layout.forgot_info);
alert.setCancelable(true);
alert.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

AutocompleteSupportFragment autocompleteSupportFragment =
   (AutocompleteSupportFragment) getSupportFragmentManager().findFragmentById(R.id.auto2);

错误还提到 android.support.v4.app.FragmentActivity.onCreateViewandroid.view.LayoutInflater.createViewFromTag

是否可能因为 Activity 认为存在多个 AutocompleteSearchFragments 而导致 重复 ID 错误?

如果是这样,我怎样才能在 Dialog 关闭后删除或删除 AutocompleteSearchFragment

我找到了问题的解决方案,对于那些以后可能会有帮助的人

为了防止Activity每次打开Dialog都认为有多个AutocompleteSearchFragment相同ID,我设置了一个onDismissListener 对于 Dialog 以删除 AutocompleteSearchFragment:

代码:

alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        getSupportFragmentManager().beginTransaction().
               remove((Fragment) autocompleteSupportFragment).commit();
    }
});

上面的代码所做的是当调用 alertDialog 被关闭时,它使用 SupportFragmentManager 删除 AutocompleteSearchFragment.