引起:java.lang.IllegalArgumentException:provider = network

时间:2012-07-15 06:46:42

标签: android

试图找到我的device.code的当前位置。

package com.example.location;

import android.app.Activity;    
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;


public class LocationActivity extends Activity
{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  /* Use the LocationManager class to obtain GPS locations */
  LocationManager mlocManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

  LocationListener mlocListener = new MyLocationListener();
  mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
}

/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{

  @Override
  public void onLocationChanged(Location loc)
  {

    loc.getLatitude();
    loc.getLongitude();

    String Text = "My current location is: " +
    "Latitud = " + loc.getLatitude() +
    "Longitud = " + loc.getLongitude();

    Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onProviderDisabled(String provider)
  {
    Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
  }

  @Override
  public void onProviderEnabled(String provider)
  {
    Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras)
  {

  }
}
}

和logcat是

07-15 12:42:49.294: E/AndroidRuntime(3322): FATAL EXCEPTION: main
07-15 12:42:49.294: E/AndroidRuntime(3322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.location/com.example.location.LocationActivity}: java.lang.IllegalArgumentException: provider=network
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.os.Looper.loop(Looper.java:137)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.app.ActivityThread.main(ActivityThread.java:4424)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at java.lang.reflect.Method.invoke(Method.java:511)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at dalvik.system.NativeStart.main(Native Method)
07-15 12:42:49.294: E/AndroidRuntime(3322): Caused by: java.lang.IllegalArgumentException: provider=network
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.os.Parcel.readException(Parcel.java:1331)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.os.Parcel.readException(Parcel.java:1281)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.location.LocationManager._requestLocationUpdates(LocationManager.java:589)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.location.LocationManager.requestLocationUpdates(LocationManager.java:453)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at com.example.location.LocationActivity.onCreate(LocationActivity.java:26)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.app.Activity.performCreate(Activity.java:4465)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-15 12:42:49.294: E/AndroidRuntime(3322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
07-15 12:42:49.294: E/AndroidRuntime(3322):     ... 11 more

2 个答案:

答案 0 :(得分:16)

我通过检查是否已启用LocationManagers NETWORK_PROVIDER来解决此问题

if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

答案 1 :(得分:1)

您是否在Real Device上尝试过相同的代码?我认为它应该适用于设备,网络提供商会在模拟器上造成问题。

编辑: - 根据@Felipe评论,许多设备可能不支持网络提供商。使用以下方法检查提供程序是否实际启用。

/*Pass context as well as Provider name, you can get provider name via LocationManager class*/
     public boolean isProviderEnabled(Context ctx, String provider){
        LocationManager manager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
        return manager.isProviderEnabled(provider);
    }