2016-12-28 52 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

感謝您的解決方案,它爲我工作。 –