AutocompleteSearchFragment in Dialog gives InflateException

喜夏-厌秋 提交于 2021-02-05 11:52:36

问题


I am using the Google Map's Places API's AutoCompleteSearchFragment in a Dialog. The error that I am getting occurs when I launch the dialog, close it, then relaunch it.

Error Message:

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

My code:

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);

The error also goes on to mention android.support.v4.app.FragmentActivity.onCreateView and android.view.LayoutInflater.createViewFromTag.

Is it possible that the duplicate id error is occurring because the Activity believes that there is more than one AutocompleteSearchFragments?

If so, how would I be able to delete or remove the AutocompleteSearchFragment once the Dialog is closed?


回答1:


I found the solution to the problem, for those that it may help later on.

In order to prevent the Activity from thinking that there is more than one AutocompleteSearchFragment with the same ID each time I open the Dialog, I set a onDismissListener for the Dialog in order to remove the AutocompleteSearchFragment:

The code:

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

What the code above does is when the Dialog called alert is dismissed, it uses the SupportFragmentManager to remove that AutocompleteSearchFragment.



来源:https://stackoverflow.com/questions/57015841/autocompletesearchfragment-in-dialog-gives-inflateexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!