2016-12-28 55 views
1

我试图使用android地图来定位指定的地址。我能够成功地在地图上找到并标记地址。但是,当我尝试找到另一个地址,它抛出的片段异常:Xamarin.Android:MapFragment中的Android.Views.InflateException当第二次启动时

二进制XML文件行#1:错误充气类片段

axml布局:

<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/myMap" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      class="com.google.android.gms.maps.MapFragment" /> 

问题是,当片段被第二次替换以找到新地址时,android无法从axml充气。它崩溃在线:

view = inflater.Inflate(Resource.Layout.map_layout, null); 

enter image description here

我通过这些链接了,但是能找到解决办法:

Android map v2 error on second inflating

Android - SupportMapFragment error when openning for second time

https://forums.xamarin.com/discussion/7378/google-maps-wont-work-error-on-fragmenttransaction

我也试过用SupportMapFragment,行为是一样的。

+0

试试这个[DEMO](https://developer.xamarin.com/samples/ mondroid/MapsAndLocationDemo_v3 /)在BasicDemoActivity中使用'MapFragment' –

+0

您可以添加更多关于您的分段事务的代码或将mvce(http://stackoverflow.com/help/mcve)上传到您的文章?我的猜测是你需要使用replace()来交换片段,或者重用片段而不是膨胀。否则,如果在“fragment”中有一个“fragment”,则需要使用“ChildFragmentMananger”。 –

+0

Jon:可能您认为交换片段是正确的。但是,除此之外,我找到了另一种方式。我已经更新了答案。可以请张贴任何引用来理解replace()的概念,重用片段。 –

回答

1

最后,搜索了很多我找到了解决方案。我没有使用MapFragment,而是使用MapView。以下是我的代码:

axml布局:

<?xml version="1.0" encoding="utf-8"?> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/myMap" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      class="com.google.android.gms.maps.MapView" /> 

片段代码:

public class MapViewFragment : Android.Support.V4.App.Fragment, IOnMapReadyCallback 
{ 
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     View view = inflater.Inflate(Resource.Layout.map_layout, null); 
     // Now this line works fine without throwing Inflate Exception 
    } 
} 
+0

感谢您的解决方案,它为我工作。 –