android中的错误处理。异常引起:java.lang.IllegalArgumentException:provider = gps

时间:2012-01-15 17:38:45

标签: android exception-handling gps location

我收到了其中一个应用的错误报告。

我几乎是Android开发的初学者,并不太明白这可能源于何处。应用程序代码在哪里可以捕获所有错误,例如只是出现错误的Toast消息?

在显示按钮之前,我的应用程序基本上处于等待GPS和/或网络位置。 这是错误:

java.lang.RuntimeException: Unable to resume activity 
{fr.mcnamara.irelandtravelguide/fr.mcnamara.irelandtravelguide.StartActivity}:
java.lang.IllegalArgumentException: provider=gps
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2241)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2256)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1789)
at android.app.ActivityThread.access$1500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: provider=gps
at android.os.Parcel.readException(Parcel.java:1326)
at android.os.Parcel.readException(Parcel.java:1276)
at android.location.ILocationManager$Stub$Proxy.requestLocationUpdates(ILocationManager.java:646)
at android.location.LocationManager._requestLocationUpdates(LocationManager.java:582)
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
at fr.mcnamara.irelandtravelguide.StartActivity.onResume(StartActivity.java:112)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
at android.app.Activity.performResume(Activity.java:3832)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2231)
... 12 more    

代码如下:

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     setContentView(R.layout.main);

     myButton = (Button) findViewById(R.id.GPS); 
     //myButton.setVisibility(INVISIBLE);

     dialog = ProgressDialog.show(this, "",  "Waiting for location...", true);
     dialog.setCancelable(true);   // 1.2
     dialog.show();         

     lm = (LocationManager) getSystemService(LOCATION_SERVICE);  
}


protected void onResume() {
    /*
     * onResume is is always called after onStart, even if the app hasn't been
     * paused
     *
     * add location listener and request updates every 1000ms or 10m
     */
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10f, this);
    super.onResume();
}

@Override
protected void onPause() {
    /* GPS, as it turns out, consumes battery like crazy */
    lm.removeUpdates(this);
    super.onResume();
}

@Override
public void onLocationChanged(Location location) {
    Log.v(TAG, "Location Changed");
    myButton.setVisibility( VISIBLE ); 
    noOfFixes++;

    /* display some of the data in the TextView */ 
     lon = location.getLongitude();
     lat = location.getLatitude();
     valsin.putDouble("lat", lat);
     valsin.putDouble("lon", lon); 
     Log.d(TAG, "set lat,lon:"+lat+","+lon);
     myButton.setVisibility(VISIBLE);
     dialog.dismiss(); 
     //txtInfo.setText("set lat,lon:"+lat+","+lon);
}

@Override
public void onProviderDisabled(String provider) {
    /* this is called if/when the GPS is disabled in settings */
    Log.v(TAG, "Disabled");
    /* bring up the GPS settings */
    Toast.makeText(this, "GPS Disabled..", Toast.LENGTH_SHORT).show();
    //Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    //startActivity(intent);
}

@Override
public void onProviderEnabled(String provider) {
    Log.d(TAG, "Enabled");
    Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    /* This is called when the GPS status alters */
    switch (status) {
    case LocationProvider.OUT_OF_SERVICE:
        Log.d(TAG, "Status Changed: Out of Service");
        //Toast.makeText(this, "Status Changed: Out of Service",
        //Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.TEMPORARILY_UNAVAILABLE:
        Log.d(TAG, "Status Changed: Temporarily Unavailable");
        //Toast.makeText(this, "Status Changed: Temporarily Unavailable",
        //Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.AVAILABLE:
        Log.d(TAG, "Status Changed: Available");
        //Toast.makeText(this, "Status Changed: Available",
        //Toast.LENGTH_SHORT).show();
        break;
    }
}

@Override
protected void onStop() {
    //finish();
    super.onStop();
}

2 个答案:

答案 0 :(得分:3)

LocationManager.GPS_PROVIDER和其他提供商不保证可用。当代码不可用时,您的代码将抛出异常。

一个解决方案是@Bourbon所描述的解决方案,当您需要的只是一个位置时,这是一种方法。

但是,如果您需要选择一个特定的位置提供者(例如用于诊断),请使用以下代码:

if (mLocationManager.getAllProviders().indexOf(LocationManager.GPS_PROVIDER) >= 0) {
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}

仅当请求的位置提供者可用时,才会注册位置更新。否则,即使其他提供商可用,也不会提供位置更新。

答案 1 :(得分:2)

首先,您的问题出现在StartActivity.java的第112行:

at fr.mcnamara.irelandtravelguide.StartActivity.onResume(StartActivity.java:112)

嗯...

在onCreate中:

mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
mBestProvider = mLocationManager.getBestProvider(criteria, true);
mLocationManager.requestLocationUpdates(mBestProvider, 1000, 10, this);

然后在onResume和onPause:

@Override
protected void onResume()
{
    if (mLocationListener != null)
    {
        mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(mBestProvider, 1000, 10, this);
    }
    super.onResume();
}

@Override
protected void onPause()
{
    mLocationManager.removeUpdates(mLocationListener);
    mLocationManager = null;
    super.onPause();
}

然后提供一些帮助您调试的建议:

在您的应用程序中使用日志:

Log.i(YOURAPPTAG, "This is what will be viewed in the logcat");

您可以使用:Log.v(),Log.d(),Log.i(),Log.w(),Log.e() for:详细,调试,信息,警告和错误

您还可以使用try / catches,在logcat中记录异常:

try
{
...
}
catch (Exception e)
{
    Log.e(APP_TAG, "STACKTRACE");
    Log.e(APP_TAG, Log.getStackTraceString(e));
}