2017-02-20 47 views
0

我一直在一個問題上工作了幾天,但我無法解決它。我需要一些幫助。我想在點擊按鈕時從活動中激活谷歌地圖的片段。Xamarin Android,我想激活一個按鈕被點擊的活動時的谷歌地圖片段

MainActivity Screenshot

MainActivity.cs下面

using Android.App; 
using Android.Content; 
using Android.Widget; 
using Android.OS; 
using Android.Views; 
using System; 
using Android.Media; 
using System.Json; 
using System.Threading.Tasks; 
using System.Net; 
using Android.Locations; 
using System.Collections.Generic; 
using System.Linq; 
using Java.Lang; 
using Newtonsoft.Json; 

namespace gpsCaution 
{ 
    [Activity(Label = "gpsCaution", MainLauncher = true, Icon = "@drawable/gpsCaution")] 
    public class MainActivity : Activity 
    { 
     ...... 
     some codes 
     ...... 

     public static MainActivity Instance; 
     protected override async void OnCreate(Bundle bundle) 
     {   
      base.OnCreate(bundle); 
      Instance = this; 
      SetContentView(Resource.Layout.Main); 

      Button mbuttonShowMap = FindViewById<Button>(Resource.Id.buttonShowMap); 

      mbuttonShowMap.Click += delegate 
      {    
       StartActivity(typeof(Harita)); //** **It crashes here** **// 
      }; 

     ...... 
     some codes 
     ...... 
} 

Main.axml碼代碼下面

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:minHeight="25px" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="10dp" 
    android:paddingTop="10dp" 
    android:id="@+id/duzen"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/txtLocation" 
     android:width="200dp" 
     android:layout_alignParentRight="true" /> 
    <TextView 
     android:text="Konumunuz:" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView1" 
     android:layout_toLeftOf="@id/txtLocation" 
     android:layout_alignTop="@id/txtLocation" 
     android:width="100dp" 
     android:layout_alignParentLeft="true" /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/txtLocation" 
     android:id="@+id/txtAddress" 
     android:width="200dp" 
     android:layout_alignParentRight="true" /> 
    <TextView 
     android:text="Adresiniz:" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView2" 
     android:layout_toLeftOf="@id/txtAddress" 
     android:layout_alignTop="@id/txtAddress" 
     android:layout_below="@id/txtLocation" 
     android:width="100dp" 
     android:layout_alignParentLeft="true" /> 
    <TextView 
     android:text="" 
     android:layout_width="wrap_content" 
     android:layout_height="85dp" 
     android:id="@+id/textEmpty" /> 
    <ProgressBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_gravity="bottom" 
     android:id="@+id/progressBar" 
     android:visibility="invisible" /> 
    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Harita Görünümü" 
     android:id="@+id/buttonShowMap" 
     android:visibility="visible" 
     android:layout_alignBottom="@id/progressBar" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="10px" 
     android:layout_marginRight="20px" /> 
</RelativeLayout> 

Harita.axml代碼下面

下面

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.Gms.Maps; 
using Android.Gms.Maps.Model; 

namespace gpsCaution 
{ 
    [Activity(Label = "Harita")] 
    public class Harita : Activity,IOnMapReadyCallback 
    { 
     public void OnMapReady(GoogleMap googleMap) 
     { 
      MarkerOptions markerOptions = new MarkerOptions(); 
      markerOptions.SetPosition(new LatLng(16.03, 108)); 
      markerOptions.SetTitle("BABALAR"); 
      googleMap.AddMarker(markerOptions); 

      //optional 
      googleMap.UiSettings.ZoomControlsEnabled = true; 
      googleMap.UiSettings.CompassEnabled = true; 
      googleMap.MoveCamera(CameraUpdateFactory.ZoomIn()); 
     }  

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 
      SetContentView(Resource.Layout.Harita); 

      MapFragment mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.googlemap); 
      mapFragment.GetMapAsync(this); 
     }  

    } 
} 

下面

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="gpsCaution.gpsCaution" android:versionCode="1" android:versionName="1.0" android:installLocation="preferExternal"> 
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="23" /> 
    <application android:label="gpsCaution" android:icon="@drawable/gpsCaution"></application> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.WRITE_GSERVICES" /> 
    <uses-permission android:name="com.google.providers.gsf.permission.READ_GSERVICES" /> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
    <uses-permission android:name="gpsCaution.gpsCaution.MAPS_RECEIVE" android:protectionLevel="signature"/> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-features android:glEsVersion="0x00020000" android:required="true" /> 

    <application android:label="gpsCaution"> 
    <meta-data android:name="com.google.android.geo.API_KEY" android:value="REMOVED" /> 
    <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 
    </application>  
</manifest> 

我的谷歌API密鑰最後AndroidManifest.xml中碼個

Harita.cs代碼是正確的,我相信,因爲它的時候我寫的代碼工作主要活動沒有第二次活動。但是當我想從主活動中調用一個google map的片段時,點擊一個按鈕時,我得到了一個錯誤崩潰「未處理的異常: Android.Views.InflateException:二進制XML文件行#1:錯誤膨脹類片段「

請幫助,提前謝謝

回答

0

您使用了錯誤的函數來啓動地圖片段。請首先在您的MyMapActivity中創建地圖片段容器,然後使用FragmentTransaction將地圖片段提交到容器。

試試下面的代碼:

在創建地圖containner您MyMapActivity

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <FrameLayout 
     android:id="@+id/map" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
</LinearLayout> 

當您啓動MyMapActivity,提交您的地圖片段到的FrameLayout:

public class MyMapActivity : Activity,IOnMapReadyCallback 
{ 
    MapFragment _mapFragment; 
    private GoogleMap _map; 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.SecondActivity); 
     InitMapFragment(); 
    }  
    private void InitMapFragment() 
    { 
     _mapFragment = FragmentManager.FindFragmentByTag("map") as MapFragment; 
     if (_mapFragment == null) 
     { 
      GoogleMapOptions mapOptions = new GoogleMapOptions() 
       .InvokeMapType(GoogleMap.MapTypeSatellite) 
       .InvokeZoomControlsEnabled(false) 
       .InvokeCompassEnabled(true); 

      FragmentTransaction fragTx = FragmentManager.BeginTransaction(); 
      _mapFragment = MapFragment.NewInstance(mapOptions); 
      fragTx.Add(Resource.Id.map, _mapFragment, "map"); 
      fragTx.Commit(); 
     } 
     _mapFragment.GetMapAsync(this); 
    } 

    public void OnMapReady(GoogleMap googleMap) 
    { 
     _map = googleMap; 
    } 
} 

開放您的MainActivity中的MyMapActivity

private void Bt1_Click(object sender, System.EventArgs e) 
    { 
     StartActivity(typeof(MyMapActivity)); 
    } 

這是你的地圖片段:

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

屏幕截圖:

GitHub source code

enter image description hereenter image description here

+0

非常感謝,現在的工作:) – eunal

+0

那麼怎麼可以添加幾個標記,如果它的簡單,位置? – eunal

+0

@eunal請參考這個[demo](https://developer.xamarin.com/samples/monodroid/MapsAndLocationDemo_v3/)它會有幫助 –

相關問題