错误的Andr​​oid处理。 java.lang.IllegalArgumentException异常:异常所致提供商= GPS [英] error handling in android. exception Caused by: java.lang.IllegalArgumentException: provider=gps

查看:1426
本文介绍了错误的Andr​​oid处理。 java.lang.IllegalArgumentException异常:异常所致提供商= GPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个错误报告我的应用程序之一。

我是pretty在很多android开发的初学者,也不太明白的地方,这可能发源于。哪里是应用code会是最好的捕捉所有错误,例如只是吐司消息的错误发生呢?

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

 了java.lang.RuntimeException:无法恢复活动
{fr.mcnamara.irelandtravelguide / fr.mcnamara.irelandtravelguide.StartActivity}:
java.lang.IllegalArgumentException异常:供应商= GPS
在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2241)
在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2256)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1789)
在android.app.ActivityThread.access $ 1500(ActivityThread.java:123)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:939)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:130)
在android.app.ActivityThread.main(ActivityThread.java:3835)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:507)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:864)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
在dalvik.system.NativeStart.main(本机方法)
java.lang.IllegalArgumentException异常:引起提供商= GPS
在android.os.Parcel.readException(Parcel.java:1326)
在android.os.Parcel.readException(Parcel.java:1276)
在android.location.ILocationManager $存根$ Proxy.requestLocationUpdates(ILocationManager.java:646)
在android.location.LocationManager._requestLocationUpdates(LocationManager.java:582)
在android.location.LocationManager.requestLocationUpdates(LocationManager.java:446)
在fr.mcnamara.irelandtravelguide.StartActivity.onResume(StartActivity.java:112)
在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
在android.app.Activity.performResume(Activity.java:3832)
在android.app.ActivityThread.performResumeActivity(ActivityThread.java:2231)
... 12更多

在code是如下:

 公共无效的onCreate(捆绑savedInstanceState){
     super.onCreate(savedInstanceState);     的setContentView(R.layout.main);     则myButton =(按钮)findViewById(R.id.GPS);
     //myButton.setVisibility(INVISIBLE);     对话框= ProgressDialog.show(这一点,,等待位置...,真正的);
     dialog.setCancelable(真); // 1.2
     dialog.show();     LM =(的LocationManager)getSystemService(LOCATION_SERVICE);
}
保护无效onResume(){
    / *
     * onResume是在onStart后总是调用,即使该应用程序还没有
     *暂停
     *
     *添加位置监听器,并要求每1000毫秒或10米更新
     * /
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,10F,本);
    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,10F,本);
    super.onResume();
}@覆盖
保护无效的onPause(){
    / * GPS,事实证明,消耗电池像疯了似的* /
    lm.removeUpdates(本);
    super.onResume();
}@覆盖
公共无效onLocationChanged(地点){
    Log.v(TAG,位置发生改变);
    myButton.setVisibility(可见);
    noOfFixes ++;    / *显示一些数据在TextView的* /
     经度= location.getLongitude();
     纬度= location.getLatitude();
     valsin.putDouble(纬度,LAT);
     valsin.putDouble(LON,LON);
     Log.d(TAG,设置纬度,经度:+纬度+,+ LON);
     myButton.setVisibility(可见);
     dialog.dismiss();
     //txtInfo.setText(\"set纬度,经度:+纬度+,+ LON);
}@覆盖
公共无效onProviderDisabled(字符串提供商){
    / *这就是所谓的,如果/当GPS在设置中禁用* /
    Log.v(TAG,已禁用);
    / *调出GPS设置* /
    Toast.makeText(这一点,GPS禁用..,Toast.LENGTH_SHORT).show();
    //意向意图=新意图(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    // startActivity(意向);
}@覆盖
公共无效onProviderEnabled(字符串提供商){
    Log.d(TAG,已启用);
    Toast.makeText(这一点,GPS已启用,Toast.LENGTH_SHORT).show();
}@覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
    / *这就是所谓的当GPS状态改变* /
    开关(状态){
    案例LocationProvider.OUT_OF_SERVICE:
        Log.d(TAG,状态更改:不在服务区);
        //Toast.makeText(this,状态更改:不在服务区
        //Toast.LENGTH_SHORT).show();
        打破;
    案例LocationProvider.TEMPORARILY_UNAVAILABLE:
        Log.d(TAG,状态更改:暂时不可用);
        //Toast.makeText(this,状态更改:暂时不可用,
        //Toast.LENGTH_SHORT).show();
        打破;
    案例LocationProvider.AVAILABLE:
        Log.d(TAG,状态更改:可用);
        //Toast.makeText(this,状态更改:可用,
        //Toast.LENGTH_SHORT).show();
        打破;
    }
}@覆盖
保护无效的onStop(){
    //完();
    super.onStop();
}


解决方案

LocationManager.GPS_PROVIDER 和其他供应商都不能保证是可用的。当他们无法使用你的code会抛出异常。

一个解决办法就是@Bourbon所描述的,这是去当你需要的是一个位置的方式。

不过,如果你需要选择一个特定的位置提供(例如,用于诊断),然后用这个code:

 如果(mLocationManager.getAllProviders()的indexOf(LocationManager.GPS_PROVIDER)> = 0){
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,这一点);
}

这将为位置更新登记仅当请求的位置提供可用。否则没有位置更新将交付,即使其他供应商可供选择。

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();
}

解决方案

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.

这篇关于错误的Andr​​oid处理。 java.lang.IllegalArgumentException异常:异常所致提供商= GPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆