2012-01-15 96 views
1

我收到一個錯誤報告我的應用程序之一。在android中的錯誤處理。 java.lang.IllegalArgumentException異常:異常所致提供商=全球定位系統

我幾乎在Android開發初學者,而不太明白的地方,這可能發源於。應用程序代碼在哪裏最好能夠捕獲所有錯誤,例如只是發送錯誤信息而已?

我的應用程序基本上位於與顯示按鈕之前等待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(); 
} 
+0

此鏈接可以提供對這個問題的一些光:http://code.google.com/p/android/issues/detail?id=19857然而,仍然不確定如何實施代碼修復...剛剛開始看看這一刻,所以任何幫助表示讚賞! – 2012-01-15 17:44:40

+0

你確定gps已開啓嗎? – JoxTraex 2012-01-15 18:31:00

回答

2

首先,你的問題是在線路112上在StartActivity.java:

在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:verbos E,調試,信息,警告和錯誤

您還可以使用你的嘗試/捕獲,登錄異常的logcat的:

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

這是線112: lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,如圖10F所示,本); – 2012-01-17 15:48:07

+0

是的,我知道,但你試過我的解決方案嗎? – Bourbon 2012-01-20 01:15:40

3

LocationManager.GPS_PROVIDER和其他供應商都不能保證是可用的。您的代碼在不可用時會拋出異常。

一種解決方案是通過@Bourbon,這是去當你需要的是一個位置的方式描述的。

但是,如果你需要選擇一個特定的位置提供商(例如,用於診斷),然後使用此代碼:

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

這將爲位置更新註冊只有在要求的位置提供程序可用。否則,即使其他提供商可用,也不會傳送位置更新。