Reputation: 421
In my application homepage The google place
auto complete fragment
opens on a button press inside a dialog bar and the location is used for a search query. The problem is when I try to change the location by pressing the button again the application crashes with the following error.
*android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment
Duplicate id 0x7f0e00db, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment*
This is the button press activity and the method that it calls.
case R.id.text_area:
EditLocation();
break;
private void EditLocation() {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.area);
dialog.setTitle("Please enter the location");
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
((EditText)autocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input)).setHint("Search Location");
//((EditText)autocompleteFragment.getView().findViewById(R.id.place_autocomplete_search_input)).setTextSize(10.0f);
//txtlocation=(TextView) findViewById(R.id.dialogLocationText);
autocompleteFragment.setOnPlaceSelectedListener(this);
Button dialogButtonSave = (Button) dialog.findViewById(R.id.btnOK);
// if button is clicked, close the custom dialog
dialogButtonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//area=dialogLocation.getText().toString();
location.setText(area);
dialog.dismiss();
//autocompleteFragment.setText("");
}
});
dialog.show();
}
Following is the xml code
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_marginTop="10dp"
android:layout_height="35dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="10dp"
android:layout_weight=".335"
android:gravity="center"
android:textSize="13sp"
android:background="@android:color/white"
>
<fragment
android:id="@+id/autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v7.widget.CardView>
<Button
android:id="@+id/btnOK"
android:layout_width="350dp"
android:layout_margin="10dp"
android:layout_height="35dp"
android:text="OK"
android:background="@drawable/custom_btn_orange"/>
</LinearLayout>
Following are screenshots of how the button and fragment operate.
Following is the logcat
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.itcastle.kleanify, PID: 3659 android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment at android.view.LayoutInflater.inflate(LayoutInflater.java:539) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) at android.app.Dialog.setContentView(Dialog.java:512) at com.itcastle.kleanify.WelcomeHomeTemp.EditLocation(WelcomeHomeTemp.java:533) at com.itcastle.kleanify.WelcomeHomeTemp.onClick(WelcomeHomeTemp.java:453) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) at android.app.Dialog.setContentView(Dialog.java:512) at com.itcastle.kleanify.WelcomeHomeTemp.EditLocation(WelcomeHomeTemp.java:533) at com.itcastle.kleanify.WelcomeHomeTemp.onClick(WelcomeHomeTemp.java:453) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.IllegalArgumentException: Binary XML file line #24: Duplicate id 0x7f0e00db, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2205) at android.app.FragmentController.onCreateView(FragmentController.java:98) at android.app.Activity.onCreateView(Activity.java:5546) at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:36) at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:75) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at android.view.LayoutInflater.inflate(LayoutInflater.java:374) at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) at android.app.Dialog.setContentView(Dialog.java:512) at com.itcastle.kleanify.WelcomeHomeTemp.EditLocation(WelcomeHomeTemp.java:533) at com.itcastle.kleanify.WelcomeHomeTemp.onClick(WelcomeHomeTemp.java:453) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) I/Process: Sending signal. PID: 3659 SIG: 9 Application terminated.
I have gone through many questions and almost everywhere the answer is to add some lines of code inside onDestroyView()
which I dont have. On the first click the fragment works perfectly, the problem is when I press the button again to change the location the application completely crushes. Any suggestion is appreciated. Thanks.
Upvotes: 1
Views: 1923
Reputation: 1346
Here is the code for the same dialog_xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="4dp"
android:layout_marginBottom="2dp"
card_view:contentPadding="1dp"
android:layout_margin="2dp"
tools:ignore="MissingPrefix"
card_view:cardBackgroundColor="#fff">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_geofence"
android:layout_width="match_parent"
android:layout_marginRight="12dp"
android:layout_marginLeft="12dp"
android:layout_height="match_parent"
tools:ignore="MissingPrefix"
tools:context="in.gpstracar.gpstracker.GeofenceActivity">
<Button
android:text="Cancel"
android:layout_width="match_parent"
fontPath="fonts/arial.ttf"
android:textColor="@color/text_color"
android:background="#fff"
android:layout_marginBottom="8dp"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:elevation="2dp"
android:layout_below="@+id/addGeofenceBtn"
android:layout_height="wrap_content"
android:id="@+id/dismissbutton1"
/>
<Button
android:text="Select"
android:layout_width="match_parent"
fontPath="fonts/arial.ttf"
android:textColor="#fff"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:background="@color/colorPrimary"
android:layout_height="wrap_content"
android:layout_below="@+id/setRadiusEditText"
android:id="@+id/submit_buttom" />
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="50dp"
android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="53dp" />
<TextView
android:text="Dialog_Title"
android:layout_width="wrap_content"
fontPath="fonts/arial.ttf"
android:textColor="@color/text_color"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:id="@+id/textView8"
android:textStyle="normal|bold" />
</RelativeLayout>
And Inflating_Dialog in activity
private void OpeningDialog(){
final SupportPlaceAutocompleteFragment places ;
dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.geo_fence_dialog);
places= (SupportPlaceAutocompleteFragment) getSupportFragmentManager()
.findFragmentById(R.id.place_autocomplete_fragment);
places.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
Toast.makeText(getApplicationContext(), place.getName() , Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Status status) {
Toast.makeText(getApplicationContext(), status.toString(), Toast.LENGTH_SHORT).show();
}
});
Button submitbutton = (Button) dialog.findViewById(R.id.submit_buttom);
submitbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction ft2 = getSupportFragmentManager()
.beginTransaction();
ft2.remove(getSupportFragmentManager()
.findFragmentById(R.id.place_autocomplete_fragment));
ft2.commit();
dialog.dismiss();
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.dismissbutton1);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentTransaction ft2 = getSupportFragmentManager()
.beginTransaction();
ft2.remove( getSupportFragmentManager()
.findFragmentById(R.id.place_autocomplete_fragment));
ft2.commit();
dialog.dismiss();
}
});
dialog.show();
}
Now Just call the method OpeningDialog()
Things to keep in mind using this you must set your dialog to setcancelable
to false
and give a button closing the dialog and removing the fragment
on that.
If you want to set setcancelable
to true
then you should handle removing of fragment on that, if you don't remove the fragment it will give the error when inflating the second time.
If any query, let me know.
Upvotes: 2
Reputation: 23881
try this in your map fragment:
public void onDestroyView() {
super.onDestroyView();
FragmentManager fm = getActivity().getSupportFragmentManager();
Fragment fragment = (fm.findFragmentById(R.id.map));
FragmentTransaction ft = fm.beginTransaction();
ft.remove(fragment);
ft.commit();
}
Upvotes: 2