2016-05-25 11 views
2

私はxamarinフォームアプリケーションからそれを消費するために、カスタムページレンダラにmapbox xamarin.android sdkを実装しようとしています。これまで私はテキストラベルでカスタムレンダラーを作成し、それを消費することができました。xamarinフォームでカスタムページレンダラーにマップボックスを実装する方法は?

//image

は、これは私のpageRendererクラスである:それはこのようになります

[assembly: ExportRenderer (typeof(MapPage), typeof(MapPageRenderer))] 
namespace mapetor.Droid 
{ 
public class MapPageRenderer : PageRenderer, TextureView.ISurfaceTextureListener 
{ 
    global::Android.Views.View view; 
    MapView map; 

    protected override void OnElementChanged(ElementChangedEventArgs<Page> e){ 
     base.OnElementChanged(e); 

     if (e.OldElement != null || Element == null) 
     { 
      return; 
     } 
     try 
     { 
      // load some android xaml 
      Task.Run(() => 
      { 
       SetupUserInterface(); 
      }).ContinueWith((rslt) => 
      { 
       AddView(view); 
       Device.BeginInvokeOnMainThread(() => { map.OnCreate(null); }); 
      }); 
     } 
     catch (Exception ex) 
     { 
      System.Diagnostics.Debug.WriteLine(ex.ToString()); 
     } 
    } 

    private async void SetupUserInterface() 
    { 
     var activity = this.Context as Activity; 
     view = activity.LayoutInflater.Inflate(Resource.Layout.mapLayout, this, false); 

     Device.BeginInvokeOnMainThread(async() => 
     { 
      // instanciando el mapa 
      map = FindViewById<MapView>(Resource.Id.mapView); 
      map.StyleUrl = Mapbox.Constants.Style.Emerald; 
      var mapboxMap = await map.GetMapAsync(); 
      var position = new CameraPosition.Builder() 
      .Target(new LatLng(41.885, -87.679)) 
      .Zoom(11) 
      .Build(); 
      mapboxMap.AnimateCamera(CameraUpdateFactory.NewCameraPosition(position), 2500); 
     }); 
    } 


    protected override void OnLayout(bool changed, int l, int t, int r, int b) 
    { 
     base.OnLayout(changed, l, t, r, b); 

     var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly); 
     var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly); 

     view.Measure(msw, msh); 
     view.Layout(0, 0, r - l, b - t); 
    } 

    public void OnSurfaceTextureAvailable(Android.Graphics.SurfaceTexture surface, int width, int height) 
    { 
     // 
    } 

    public bool OnSurfaceTextureDestroyed(Android.Graphics.SurfaceTexture surface) 
    { 
     // 
     return false; 
    } 

    public void OnSurfaceTextureSizeChanged(Android.Graphics.SurfaceTexture surface, int width, int height) 
    { 
     // 
    } 

    public void OnSurfaceTextureUpdated(Android.Graphics.SurfaceTexture surface) 
    { 
     // 
    } 

    public Bundle savedInstanceState { get; set; } 
} 

}

、これは私のmapLayout.xamlファイルです:しかし

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:background="#fcfcfc" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_gravity="center"> 
<TextView 
    android:text="Mapbox test" 
    android:textColor="#404040" 
    android:textSize="60dp" 
    android:gravity="center" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
<com.mapbox.mapboxsdk.maps.MapView xmlns:mapbox="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/mapView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    mapbox:access_token="MY_ACCESS_TOKEN" /> 
</LinearLayout> 

私がそれを実行すると、このエラーが発生します:

05-25 13:56:54.287 E/mono (16862): Unhandled Exception: 
    05-25 13:56:54.287 E/mono (16862): Android.Views.InflateException: Binary  XML file line #1: Binary XML file line #1: Error inflating class com.mapbox.mapboxsdk.maps.MapView ---> Android.Views.InflateException: Binary XML file line #1: Error inflating class com.mapbox.mapboxsdk.maps.MapView ---> Java.Lang.Reflect.InvocationTargetException: Exception of type 'Java.Lang.Reflect.InvocationTargetException' was thrown. ---> Android.Views.InflateException: Binary XML file line #1: Binary XML file line #1: Error inflating class com.mapbox.mapboxsdk.maps.widgets.UserLocationView ---> Android.Views.InflateException: Binary XML file line #1: Error inflating class com.mapbox.mapboxsdk.maps.widgets.UserLocationView ---> Java.Lang.Reflect.InvocationTargetException: Exception of type 'Java.Lang.Reflect.InvocationTargetException' was thrown. ---> Java.Lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
05-25 13:56:54.287 E/mono (16862): 
05-25 13:56:54.287 E/mono (16862): --- End of managed exception stack trace --- 
05-25 13:56:54.287 E/mono (16862): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
05-25 13:56:54.287 E/mono (16862): at android.os.Handler.<init>(Handler.java:200) 
05-25 13:56:54.287 E/mono (16862): at android.os.Handler.<init>(Handler.java:114) 
05-25 13:56:54.287 E/mono (16862): at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:227) 
05-25 13:56:54.287 E/mono (16862): at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:227) 
05-25 13:56:54.287 E/mono (16862): at android.location.LocationManager.wrapListener(LocationManager.java:864) 
05-25 13:56:54.287 E/mono (16862): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:877) 
05-25 13:56:54.287 E/mono (16862): at android.location.LocationManager.requestLocationUpdates(LocationManager.java:464) 
05-25 13:56:54.287 E/mono (16862): at com.mapzen.android.lost.internal.FusionEngine.enablePassive(FusionEngine.java:118) 
05-25 13:56:54.287 E/mono (16862): at com.mapzen.android.lost.internal.FusionEngine.enable(FusionEngine.java:82) 
05-25 13:56:54.287 E/mono (16862): at com.mapzen.android.lost.internal.LocationEngine.setRequest(LocationEngine.java:36) 
05-25 13:56:54.287 E/mono (16862): at com.mapzen.android.lost.internal.FusedLocationProviderApiImpl.requestLocationUpdates(FusedLocationProviderApiImpl.java:38) 
05-25 13:56:54.287 E/mono (16862): at com.mapbox.mapboxsdk.location.LocationServices.toggleGPS(LocationServices.java:104) 
05-25 13:56:54.287 E/mono (16862): at com.mapbox.mapboxsdk.maps.widgets.UserLocationView.toggleGps(UserLocationView.java:378) 
05-25 13:56:54.287 E/mono (16862): at com.mapbox.mapboxsdk.maps.widgets.UserLocationView.setEnabled(UserLocationView.java:251) 
05-25 13:56:54.287 E/mono (16862): at com.mapbox.mapboxsdk.maps.widgets.UserLocationView.initialize(UserLocationView.java:119) 
05-25 13:56:54.287 E/mono (16862): at com.mapbox.mapboxsdk.maps.widgets.UserLocationView.<init>(UserLocationView.java:109) 
05-25 13:56:54.287 E/mono (16862): at java.lang.reflect.Constructor.newInstance(Native Method) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
05-25 13:56:54.287 E/mono (16862): at com.mapbox.mapboxsdk.maps.MapView.initialize(MapView.java:179) 
05-25 13:56:54.287 E/mono (16862): at com.mapbox.mapboxsdk.maps.MapView.<init>(MapView.java:158) 
05-25 13:56:54.287 E/mono (16862): at java.lang.reflect.Constructor.newInstance(Native Method) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
05-25 13:56:54.287 E/mono (16862): 
05-25 13:56:54.287 E/mono (16862): --- End of inner exception stack trace --- 
05-25 13:56:54.287 E/mono (16862): 
05-25 13:56:54.287 E/mono (16862): --- End of managed exception stack trace --- 
05-25 13:56:54.287 E/mono (16862): java.lang.reflect.InvocationTargetException 
05-25 13:56:54.287 E/mono (16862): at java.lang.reflect.Constructor.newInstance(Native Method) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
05-25 13:56:54.287 E/mono (16862): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704 
05-25 13:56:54.291 D/OpenGLRenderer(16862): Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
referenceTable GDEF length=814 1 
referenceTable GSUB length=11364 1 
referenceTable GPOS length=47302 1 

私はerorrは知っている「Looper.prepare(と呼ばれていないスレッド内でハンドラを作成することはできませんが)、」UIスレッド上で実行されていないコードに関連する研究しているが、それはまた、それが失敗したと言いますマップボックスウィジェットを見つける。

mapbox xamarinのドキュメントの例は、マップがアクティビティ内で使用されているときを指しますが、ここではカスタムレンダラの一部として実行時にマップを作成していますので、de onCreate()onPause )onDestroy()..などの関数。

だから、私は何が不足しているか、多分私はこれをすべて間違ってやっています。

答えて

1

Android.Views.InflateException:バイナリXMLファイルのライン#1:エラー膨らまクラスcom.mapbox.mapboxsdk.maps.widgets.UserLocationView ---> Java.Lang.RuntimeException:スレッド内でハンドラを作成できませんそれはLooper.prepare呼び出されていない()

メインスレッドのデリゲート内でごビューインフレを移動してみてください:

private async void SetupUserInterface() 
{ 
    var activity = this.Context as Activity; 
    Device.BeginInvokeOnMainThread(async() => 
    { 
     view = activity.LayoutInflater.Inflate(Resource.Layout.mapLayout, this, false); 
     // instanciando el mapa 
     map = FindViewById<MapView>(Resource.Id.mapView); 
     map.StyleUrl = Mapbox.Constants.Style.Emerald; 
     var mapboxMap = await map.GetMapAsync(); 
     var position = new CameraPosition.Builder() 
     .Target(new LatLng(41.885, -87.679)) 
     .Zoom(11) 
     .Build(); 
     mapboxMap.AnimateCamera(CameraUpdateFactory.NewCameraPosition(position), 2500); 
    }); 
} 
関連する問題