强制关闭应用程序,何时获取地址..NULL异常

发布于 2025-01-08 22:22:38 字数 3965 浏览 4 评论 0原文

我试图通过 ihrupin 从 http://www.hrupin.com/2011/04/android-gps-using-how-to-get-current-location-example

它工作正常,直到我添加通过地理编码在 onLocationChanged 中获取地址形式的纬度和经度

这是代码:


buttonGetLocation.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                progress.setVisibility(View.VISIBLE);
                // exceptions will be thrown if provider is not permitted.
                try 
                {
                    gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                } 
                catch (Exception ex) 
                {
                }
                try 
                {
                    network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                } 
                catch (Exception ex) 
                {
                }

                // don't start listeners if no provider is enabled      

                if (gps_enabled) 
                {
                    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
                }
                if (network_enabled) 
                {
                    locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
                }
            }
        });

        locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    }



    class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                // This needs to stop getting the location data and save the battery power.
                locManager.removeUpdates(locListener); 

                String londitude = "Londitude: " + location.getLongitude();
                String latitude = "Latitude: " + location.getLatitude();
                String altitiude = "Altitiude: " + location.getAltitude();
                String accuracy = "Accuracy: " + location.getAccuracy();
                String time = "Time: " + location.getTime();

                //
                String address = "Adress : "+goToGeocoder(location.getLatitude().getLongitude());

                editTextShowLocation.setText(londitude + "\n" + latitude + "\n" + altitiude + "\n" + accuracy + "\n" + address);
                progress.setVisibility(View.GONE);
            } 
        }
Context context;
    public String goToGeocoder(String ll)
    {
        List<Address> almt;
        String alamat = null;
        double lat,lng;
        StringBuilder sb = new StringBuilder();
        lat = Double.parseDouble(ll.substring(0,ll.indexOf(",")-1));
        lng = Double.parseDouble(ll.substring(ll.indexOf(",")+1));

        try
        {
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());
            almt = geocoder.getFromLocation(lat, lng, 5);
            if(almt.size()>0)
            {
                Address almtSkrng = almt.get(0);
                String admin_area = sb.append(almtSkrng.getAdminArea()).toString();
                if(almtSkrng.getAddressLine(0).equals(null) || almtSkrng.getAddressLine(0).equals(""))
                {
                    alamat = almtSkrng.getFeatureName();
                }
                alamat = almtSkrng.getAddressLine(0)+" "+almtSkrng.getAdminArea();
                //temp_koordinate.setAlamat(alamat);
            }
            else
            {
                alamat = "";
                //temp_koordinate.setAlamat(alamat);
            }
        }       
        catch(IOException e)
        {
            //
        }
        return alamat;
    }

应用程序被强制关闭。

任何人都可以给出解决这个问题的解决方案吗? 非常感谢 亲切的问候

I'm trying to get a address with using this code by ihrupin from http://www.hrupin.com/2011/04/android-gps-using-how-to-get-current-location-example

It works fine until I add geocode to get an address form latitude and longitude in onLocationChanged

Here is the code :


buttonGetLocation.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                progress.setVisibility(View.VISIBLE);
                // exceptions will be thrown if provider is not permitted.
                try 
                {
                    gps_enabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                } 
                catch (Exception ex) 
                {
                }
                try 
                {
                    network_enabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
                } 
                catch (Exception ex) 
                {
                }

                // don't start listeners if no provider is enabled      

                if (gps_enabled) 
                {
                    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locListener);
                }
                if (network_enabled) 
                {
                    locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locListener);
                }
            }
        });

        locManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    }



    class MyLocationListener implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                // This needs to stop getting the location data and save the battery power.
                locManager.removeUpdates(locListener); 

                String londitude = "Londitude: " + location.getLongitude();
                String latitude = "Latitude: " + location.getLatitude();
                String altitiude = "Altitiude: " + location.getAltitude();
                String accuracy = "Accuracy: " + location.getAccuracy();
                String time = "Time: " + location.getTime();

                //
                String address = "Adress : "+goToGeocoder(location.getLatitude().getLongitude());

                editTextShowLocation.setText(londitude + "\n" + latitude + "\n" + altitiude + "\n" + accuracy + "\n" + address);
                progress.setVisibility(View.GONE);
            } 
        }
Context context;
    public String goToGeocoder(String ll)
    {
        List<Address> almt;
        String alamat = null;
        double lat,lng;
        StringBuilder sb = new StringBuilder();
        lat = Double.parseDouble(ll.substring(0,ll.indexOf(",")-1));
        lng = Double.parseDouble(ll.substring(ll.indexOf(",")+1));

        try
        {
            Geocoder geocoder = new Geocoder(context, Locale.getDefault());
            almt = geocoder.getFromLocation(lat, lng, 5);
            if(almt.size()>0)
            {
                Address almtSkrng = almt.get(0);
                String admin_area = sb.append(almtSkrng.getAdminArea()).toString();
                if(almtSkrng.getAddressLine(0).equals(null) || almtSkrng.getAddressLine(0).equals(""))
                {
                    alamat = almtSkrng.getFeatureName();
                }
                alamat = almtSkrng.getAddressLine(0)+" "+almtSkrng.getAdminArea();
                //temp_koordinate.setAlamat(alamat);
            }
            else
            {
                alamat = "";
                //temp_koordinate.setAlamat(alamat);
            }
        }       
        catch(IOException e)
        {
            //
        }
        return alamat;
    }

The application was force closed.

Anyone can give the solution to fix this problem?
Many thanks
kind regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§对你不离不弃 2025-01-15 22:22:38

如果不提供显示崩溃的日志(即堆栈跟踪),则很难回答强制关闭的原因...

如果没有该信息,答案可能是:

您需要处理地理编码返回 null 的情况。

if(almt != null) {
    //do what you need to do
} else {
    //handle the null situation here
}

或者您可以在 try/catch 语句中添加一个 catch 块来处理 NullPointerException。

Hard to answer the reason for your Force Close without you providing logs that show the crash (i.e. stack trace)...

Without that information, the answer might be:

You need to handle the situation whereby geocoding returns null.

if(almt != null) {
    //do what you need to do
} else {
    //handle the null situation here
}

OR you could add a catch block to your try/catch statement to handle NullPointerException.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文