提供商更改为GPS_PROVIDER时强制关闭

时间:2012-02-24 10:05:55

标签: android location

我尝试在trhead中使用NETWORK_PROVIDER获取curent坐标和地址,但是当我更改为GPS_PROVIDER时,应用程序强制关闭:使用来自LogCat的消息:

  

02-24 16:54:00.249:E / AndroidRuntime(3004):   android.view.ViewRoot $ CalledFromWrongThreadException:只有   创建视图层次结构的原始线程可以触及其视图。

我认为,当我将提供商从网络更改为GPS时,与以前使用网络时一样,应用程序运行良好的情况有何不同...

这是线程的代码:

private void requestPosition()
    {
        if(!cekInternet())
        {
            Txt_konek.setText("Need internet Connection");
        }
        else
        {
            new Thread()
            {
                public void run()
                {
                    Message localMessage = Message.obtain();
                    localMessage.what=1;
                    try
                    {
                        Splash2.this.ll = Splash2.this.show_current_location();
                        Splash2.this.lat = Double.valueOf(Double.parseDouble(Splash2.this.ll.substring(0,Splash2.this.ll.indexOf(",")-1)));
                        Splash2.this.lng = Double.valueOf(Double.parseDouble(Splash2.this.ll.substring(1+Splash2.this.ll.indexOf(","))));
                        Splash2.this.posisiUser = Splash2.this.goToGeocoder(Splash2.this.lat.doubleValue(), Splash2.this.lng.doubleValue());
                        Splash2.this.messageHandler.sendMessage(localMessage);
                        return;
                    }
                    catch(Exception localException)
                    {
                        while(true)
                        {
                Splash2.this.Txt_konek.setText("no internet connection");                           
                        }
                    }
                }
            }
            .start();
        }
    }

此方法获取当前位置:

private String show_current_location()
    {
        Location localLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(localLocation==null)
        {               
            this.longLat = localLocation.getLatitude()+","+localLocation.getLongitude();
        }
        else
        {
            this.longLat = localLocation.getLatitude()+","+localLocation.getLongitude();
        }
        return this.longLat;
    }

这是locationListener:

2 个答案:

答案 0 :(得分:0)

如果您的位置localLocation为null,则将在

处抛出空指针异常
 localLocation.getLatitude()

答案 1 :(得分:0)

GPS提供商需要时间来修复,并且您之前正在处理show_current_location方法,因此您收到了错误消息。你需要等到收到lat和lang