问题
I've received an error report for one of my apps.
I'm pretty much a beginner in android development, and don't quite understand where this could originate from. Where is the app code would it be best to catch all errors and for example just toast message that an error occured?
My app basically sits and waits for GPS and/or Network location before displaying a button. This is the error:
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
The code is as follows:
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();
}
回答1:
LocationManager.GPS_PROVIDER
and other providers are not guaranteed to be available. Your code will throw an exception when they are not available.
One solution is the one described by @Bourbon, which is the way to go when all you need is a location.
However, if you need to select one particular location provider (e.g. for diagnosis), then use this code:
if (mLocationManager.getAllProviders().indexOf(LocationManager.GPS_PROVIDER) >= 0) {
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
This will register for location updates only if the requested location provider is available. Otherwise no location updates will be delivered, even if other providers are available.
回答2:
Firstly, your problem is on line 112 in StartActivity.java :
at fr.mcnamara.irelandtravelguide.StartActivity.onResume(StartActivity.java:112)
Well...
In the onCreate :
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
mBestProvider = mLocationManager.getBestProvider(criteria, true);
mLocationManager.requestLocationUpdates(mBestProvider, 1000, 10, this);
Then in onResume and 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();
}
Then some advices to help you debugging :
Use logs in your application :
Log.i(YOURAPPTAG, "This is what will be viewed in the logcat");
You can use : Log.v(), Log.d(), Log.i(), Log.w(), Log.e() for : verbose, debug, information, warn and error
You can also use for your try/catches, to log the exception in the logcat :
try
{
...
}
catch (Exception e)
{
Log.e(APP_TAG, "STACKTRACE");
Log.e(APP_TAG, Log.getStackTraceString(e));
}
来源:https://stackoverflow.com/questions/8871825/error-handling-in-android-exception-caused-by-java-lang-illegalargumentexcepti