1

概括

我在真实设备上运行此代码,而不是模拟器。

我一直在阅读 Xamarin Monodroid 定位服务教程 -这里

当我编译代码时,我在以下代码行中得到“Java.Lang.IllegalArgumentException” -

protected override void OnResume()
        {
            base.OnResume();
            _locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
            Log.Debug(LogTag, "Listening for location updates using " + _locationProvider + ".");
        }

深入研究异常会发现以下内容 -

Java.Lang.IllegalArgumentException: provider=
  at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr,Android.Runtime.JValue[]) [0x00023] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:368
  at Android.Locations.LocationManager.RequestLocationUpdates (string,long,single,Android.Locations.ILocationListener) [0x0004a] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.Locations.LocationManager.cs:814
  at com.xamarin.recipes.getlocation.Activity1.OnResume () [0x0001b] in /Users/SimonGilbert/Downloads/GetLocation/Activity1.cs:86
  at Android.App.Activity.n_OnResume (intptr,intptr) [0x00009] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.2-branch/a25a31d0/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.App.Activity.cs:2596
  at at (wrapper dynamic-method) object.383877df-2ff3-46a8-906d-6d9bd2915e69 (intptr,intptr) <IL 0x00011, 0x0001b>
  at 
  at --- End of managed exception stack trace ---
  at java.lang.IllegalArgumentException: provider=
  at    at android.os.Parcel.readException(Parcel.java:1331)
  at    at android.os.Parcel.readException(Parcel.java:1281)
  at    at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
  at    at android.location.LocationManager._requestLocationUpdates(LocationManager.java:614)
  at    at android.location.LocationManager.requestLocationUpdates(LocationManager.java:478)
  at    at com.xamarin.recipes.getlocation.Activity1.n_onResume(Native Method)
  at    at com.xamarin.recipes.getlocation.Activity1.onResume(Activity1.java:43)
  at    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
  at    at android.app.Activity.performResume(Activity.java:4540)
  at    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2437)
  at    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2475)
  at    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1989)
  at    at android.app.ActivityThread.access$600(ActivityThread.java:124)
  at    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1148)
  at    at android.os.Handler.dispatchMessage(Handler.java:99)
  at    at android.os.Looper.loop(Looper.java:137)
  at    at android.app.ActivityThread.main(ActivityThread.java:4436)
  at    at java.lang.reflect.Method.invokeNative(Native Method)
  at    at java.lang.reflect.Method.invoke(Method.java:511)
  at    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  at    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  at    at dalvik.system.NativeStart.main(Native Method)
  at 

问题

  1. 有没有人见过这个?
  2. 关于它可能是什么原因的任何想法?

更新

  • 从阅读中,似乎添加以下内容可能会解决我的问题,尽管我还没有机会测试它 -

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
  • 似乎另一种解决方案可能是检查网络提供商是否也可用 -

    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 300000, 500, locationListener);
    
  • 也许还要检查 GPS Provider 是否也可用 -

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 500, locationListener);    
    

非常感谢。

4

2 回答 2

0

传入一个空的标准集,以及在 AssemblyInfo 文件中设置权限。

LocationManager mgr = (LocationManager) GetSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String best = mgr.GetBestProvider(criteria, true);
//since you are using true as the second parameter, you will only get the best of providers which are enabled.
Location location = mgr.GetLastKnownLocation(best);

<uses-permission android:name="android.permission.NETWORK" />
于 2013-11-06T12:52:51.433 回答
0

我在 OnCreate 中以不同的方式完成了

    btnDone = FindViewById<Button> (Resource.Id.btnDone);
        LocationManager LC=(LocationManager) GetSystemService(Context.LocationService);
        Criteria criteria=new Criteria();
        string  provider=LC.GetBestProvider(criteria, true);
        Location loc=LC.GetLastKnownLocation(provider);
`btnDone.Click += delegate {
OnLocationChanged(loc);
}

// outside OnCreate
public void OnLocationChanged(Location location) {

        double  lattitude = location.Latitude;
        double longitude = location.Longitude;
        Toast.MakeText (this, "Lattitude and Longitude is "+lattitude.ToString()+" and "+longitude.ToString(), ToastLength.Long).Show ();
    }

还实现 ILocationListener。参考这个 http://docs.xamarin.com/recipes/android/os_device_resources/gps/get_current_device_location/ ``

于 2014-05-06T12:55:26.790 回答