Android InflateException Error in fragment layout with SupportMapFragment (recreated fragment) -
all more or less works. if locationfragment recreated, application falls.
manifest
<?xml version="1.0" encoding="utf-8"?> <manifest package="xx.xx.xxxxxxxx" xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.get_accounts"/> <uses-permission android:name="android.permission.internet"/> <uses-permission android:name="android.permission.access_fine_location"/> <uses-permission android:name="android.permission.camera"/> <uses-permission android:name="android.permission.write_external_storage"/> <uses-permission android:name="android.permission.read_external_storage"/> <uses-permission android:name="android.permission.access_network_state"/> <uses-feature android:glesversion="0x00020000" android:required="true"/> <uses-feature android:name="android.hardware.camera" android:required="true" /> <application android:name=".app" android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme"> <meta-data android:name="com.google.android.geo.api_key" android:value="@string/google_maps_key"/> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/> <activity android:name=".ui.activities.launchactivity" android:label="@string/app_name" android:screenorientation="portrait" android:theme="@style/apptheme.fullscreen" android:windowsoftinputmode="statealwayshidden|adjustresize"> <intent-filter> <action android:name="android.intent.action.main"/> <category android:name="android.intent.category.launcher"/> </intent-filter> </activity> <activity android:name=".ui.activities.mainactivity" android:screenorientation="portrait" android:configchanges="keyboard|keyboardhidden|screenlayout|screensize|orientation"/> <activity android:name=".ui.activities.settingsactivity" android:screenorientation="portrait" android:configchanges="keyboard|keyboardhidden|screenlayout|screensize|orientation"/> <activity android:name="com.facebook.facebookactivity" android:configchanges="keyboard|keyboardhidden|screenlayout|screensize|orientation" android:theme="@android:style/theme.translucent.notitlebar" android:label="@string/app_name" /> <meta-data android:name="com.crashlytics.apikey" android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/> <meta-data android:name="com.facebook.sdk.applicationid" android:value="@string/facebook_app_id"/> </application>
public enum geolocationstate
import android.content.context; import android.location.address; import android.location.geocoder; import android.location.location; import android.location.locationmanager; import android.widget.toast; import com.google.android.gms.maps.cameraupdatefactory; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.model.cameraposition; import com.google.android.gms.maps.model.latlng; import java.io.ioexception; import java.util.list; import java.util.locale; public enum geolocationstate { inst; private static final string tag = "geolocation"; public static final long waiting_time_millis = 3000l; public static final float accurancy_in_meters = 3f; private context context; private locationmanager locationmanager; private geocoder geocoder; public void init(context context) { this.context = context; locationmanager = (locationmanager) context.getsystemservice(context.location_service); geocoder = new geocoder(context, locale.getdefault()); } public address getaddress(location location) { try { list<address> addresslist = geocoder.getfromlocation(location.getlatitude(), location.getlongitude(), 1); if (addresslist != null && !addresslist.isempty()) { return addresslist.get(0); } } catch (ioexception e) { e.printstacktrace(); toast.maketext(context, "Помилка геопозиціонування", toast.length_short).show(); } return null; } public void mappositioning(googlemap mmap, double latitude, double longitude){ latlng coordinates = new latlng(latitude, longitude); if(mmap!=null) { mmap.animatecamera(cameraupdatefactory.newlatlngzoom(coordinates, 13)); cameraposition cameraposition = new cameraposition.builder() .target(coordinates) .zoom(17) .bearing(90) .tilt(0) .build(); mmap.animatecamera(cameraupdatefactory.newcameraposition(cameraposition)); } } public locationmanager getlocationmanager() { return locationmanager; } }
public class locationfragment
import android.location.address; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.gravity; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.linearlayout; import android.widget.textview; import android.widget.toast; import com.google.android.gms.maps.googlemap; import com.google.android.gms.maps.onmapreadycallback; import com.google.android.gms.maps.supportmapfragment; import java.text.decimalformat; import butterknife.bindview; import butterknife.butterknife; import butterknife.unbinder; import xx.xx.xxxxxxxx.r; import xx.xx.xxxxxxxx.services.claimstate; import xx.xx.xxxxxxxx.services.geolocationstate; import xx.xx.xxxxxxxx.ui.activities.mainactivity; public class locationfragment extends basefragment implements onmapreadycallback, googlemap.onmylocationbuttonclicklistener{ private static final string tag = locationfragment.class.getname(); private googlemap mmap; @bindview(r.id.positioning_text_view) textview positioningtext; @bindview(r.id.next_button) button nextbutton; private unbinder unbinder; public static fragment newinstance() { return new locationfragment(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_location, container, false); //**inflate error in rootview when move between fragments** unbinder = butterknife.bind(this, rootview); supportmapfragment fragment = (supportmapfragment) getchildfragmentmanager().findfragmentbyid(r.id.map); fragment.getmapasync(this); return rootview; } @override public void onviewcreated(view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); nextbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { ((mainactivity) getactivity()).movetonext(); } }); nextbutton.setvisibility(view.gone); } @override public void onresume() { super.onresume(); geolocationstate.inst.getlocationmanager().requestlocationupdates( locationmanager.gps_provider, geolocationstate.waiting_time_millis, geolocationstate.accurancy_in_meters, locationlistener); } @override public void onpause() { super.onpause(); geolocationstate.inst.getlocationmanager().removeupdates(locationlistener); } private locationlistener locationlistener = new locationlistener() { @override public void onlocationchanged(location location) { if(location != null && mmap != null) { address address = geolocationstate.inst.getaddress(location); if (address != null) { claimstate.inst.getclaim().setcity(address.getlocality()); claimstate.inst.getclaim().setaddress(address.getaddressline(0)); positioningtext.settext(claimstate.inst.getfulladdress()); nextbutton.setvisibility(view.visible); } decimalformat df = new decimalformat("#.######"); claimstate.inst.getclaim().setlatitude(df.format(location.getlatitude()).replace(",", ".")); claimstate.inst.getclaim().setlongitude(df.format(location.getlongitude()).replace(",", ".")); geolocationstate.inst.mappositioning(mmap, location.getlatitude(), location.getlongitude()); } } @override public void onproviderdisabled(string provider) { } @override public void onproviderenabled(string provider) { } @override public void onstatuschanged(string provider, int status, bundle extras) { } }; @override public void onmapready(googlemap googlemap) { mmap = googlemap; if(mmap != null) { mmap.setmylocationenabled(true); mmap.setonmylocationbuttonclicklistener(this); mmap.getuisettings().setcompassenabled(false); } } @override public boolean onmylocationbuttonclick() { return false; } @override public void setuservisiblehint(boolean isvisibletouser) { super.setuservisiblehint(isvisibletouser); if (isvisibletouser && isresumed()){ toast toast = toast.maketext(getcontext(), "bla-bla-bla", toast.length_long); linearlayout layout = (linearlayout) toast.getview(); if (layout.getchildcount() > 0) { textview tv = (textview) layout.getchildat(0); tv.setgravity(gravity.center_vertical | gravity.center_horizontal); } toast.setgravity(gravity.center, 0, 0); toast.show(); } } @override public void ondestroyview() { super.ondestroyview(); unbinder.unbind(); } }
fragment_location.xml (layout)
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <relativelayout android:id="@+id/bottompanel" android:layout_width="match_parent" android:layout_height="130dp" android:layout_alignparentbottom="true" android:background="@color/primarycolor"> <textview android:id="@+id/positioning_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margintop="10dp" android:text="@string/positioning_in_progress" android:textcolor="@android:color/white" android:textsize="17sp"/> <button android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_marginbottom="20dp" android:layout_marginright="20dp" android:background="@drawable/button_background" android:paddingleft="45dp" android:paddingright="45dp" android:text="@string/next" android:textallcaps="false" android:textsize="17sp" /> </relativelayout> <fragment android:id="@+id/map" class="com.google.android.gms.maps.supportmapfragment" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@+id/bottompanel" android:layout_alignparenttop="true" /> </relativelayout>
printstacktrace
07-28 13:17:57.257 28765-28765/xx.xx.xxxxxxxx e/androidruntime: fatal exception: main process: xx.xx.xxxxxxxx, pid: 28765 android.view.inflateexception: binary xml file line #40: binary xml file line #40: error inflating class fragment @ android.view.layoutinflater.inflate(layoutinflater.java:539) @ android.view.layoutinflater.inflate(layoutinflater.java:423) @ xx.xx.xxxxxxxx.ui.fragments.locationfragment.oncreateview(locationfragment.java:55) @ android.support.v4.app.fragment.performcreateview(fragment.java:1974) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1067) @ android.support.v4.app.fragmentmanagerimpl.attachfragment(fragmentmanager.java:1430) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:732) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1617) @ android.support.v4.app.fragmentmanagerimpl.executependingtransactions(fragmentmanager.java:570) @ android.support.v4.app.fragmentpageradapter.finishupdate(fragmentpageradapter.java:141) @ android.support.v4.view.viewpager.populate(viewpager.java:1177) @ android.support.v4.view.viewpager.populate(viewpager.java:1025) @ android.support.v4.view.viewpager$3.run(viewpager.java:254) @ android.view.choreographer$callbackrecord.run(choreographer.java:858) @ android.view.choreographer.docallbacks(choreographer.java:670) @ android.view.choreographer.doframe(choreographer.java:603) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:844) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: android.view.inflateexception: binary xml file line #40: error inflating class fragment @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:782) @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:704) @ android.view.layoutinflater.rinflate(layoutinflater.java:835) @ android.view.layoutinflater.rinflatechildren(layoutinflater.java:798) @ android.view.layoutinflater.inflate(layoutinflater.java:515) @ android.view.layoutinflater.inflate(layoutinflater.java:423) @ xx.xx.xxxxxxxx.ui.fragments.locationfragment.oncreateview(locationfragment.java:55) @ android.support.v4.app.fragment.performcreateview(fragment.java:1974) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1067) @ android.support.v4.app.fragmentmanagerimpl.attachfragment(fragmentmanager.java:1430) @ android.support.v4.app.backstackrecord.run(backstackrecord.java:732) @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1617) @ android.support.v4.app.fragmentmanagerimpl.executependingtransactions(fragmentmanager.java:570) @ android.support.v4.app.fragmentpageradapter.finishupdate(fragmentpageradapter.java:141) @ android.support.v4.view.viewpager.populate(viewpager.java:1177) @ android.support.v4.view.viewpager.populate(viewpager.java:1025) @ android.support.v4.view.viewpager$3.run(viewpager.java:254) @ android.view.choreographer$callbackrecord.run(choreographer.java:858) @ android.view.choreographer.docallbacks(choreographer.java:670) @ android.view.choreographer.doframe(choreographer.java:603) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:844) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5417) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) caused by: java.lang.illegalargumentexception: binary xml file line #40: duplicate id 0x7f0e0104, tag null, or parent id 0xffffffff fragment com.google.android.gms.maps.supportmapfragment @ android.support.v4.app.fragmentmanagerimpl.oncreateview(fragmentmanager.java:2293) @ android.support.v4.view.layoutinflatercompathc$factorywrapperhc.oncreateview(layoutinflatercompathc.java:44) @ android.view.layoutinflater$factorymerger.oncreateview(layoutinflater.java:186) @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:746) @ android.view.layoutinflater.createviewfromtag(layoutinflater.java:704) @ android.view.layoutinflater.rinflate(layoutinflater.java:835) @ android.view.layoutinflater.rinflatechildren(layoutinflater.java:798) @ android.view.layoutinflater.inflate(layoutinflater.java:515) @ android.view.layoutinflater.inflate(layoutinflater.java:423) @ xx.xx.xxxxxxxx.ui.fragments.locationfragment.oncreateview(locationfragment.java:55) @ android.support.v4.app.fragment.performcreateview(fragment.java:1974) @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1067) @ android.support.v4.app.fragmentmanagerimpl.attachfragment(fragmentmanager.java:1430) .....
upd: have 4 fragments
capturefragment(1) - typefragment(2) - locationfragment(3) - overviewfragment(4)
when move between:
typefragment - locationfragment
or
locationfragment - overviewfragment
all ok.
but if move capturefragment(1) -> typefragment(2) -> locationfragment(3)
then
locationfragment(3) -> typefragment(2) -> capturefragment(1)
then
capturefragment(1) -> typefragment(2)
it breaks.
i know when typefragment created, created , next activity capturefragment. @ point, application falls.
does know how fix it?
this how load map layout in fragment:
private view view; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment if (view != null) { viewgroup parent = (viewgroup) view.getparent(); if (parent != null) parent.removeview(view); } try { view = inflater.inflate(r.layout.map_fragment_location, container, false); } catch (inflateexception e) { /* map there, return view */ } butterknife.bind(this, view); supportmapfragment mapfragment = (supportmapfragment)getchildfragmentmanager().findfragmentbyid(r.id.map); mapfragment.getmapasync(this); return view; }
this helps check layout , if exists, don't have create again!
please try , let me know how goes!
Comments
Post a Comment