如何通过点击按钮获取用户的GPS坐标?

发布于 2024-11-18 08:04:06 字数 1811 浏览 2 评论 0原文

您好,我在这里遇到问题

,我有一个用于我的观点数据库的添加和编辑表单 它有两个用于坐标的 EditText,分别用于经度和纬度 它有一个按钮来获取当前坐标 如果我单击按钮,两个 EditText 就会填充坐标 用户可以继续插入或编辑数据,

我的代码返回 java.nullPointerException,我不知道为什么......

这是我的代码:

btn_getkoor.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                try{

                    LocationListener gpsLocation = new MyLocationListener();
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, gpsLocation); //new MyLocationListener());
                    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                    final TextView error_text = (TextView) findViewById(R.id.error_text);

                    if (location != null) {

                        EditText et_longitude = (EditText) findViewById(R.id.et_longitude);
                        EditText et_latitude = (EditText) findViewById(R.id.et_latitude);

                        et_longitude.setText(""+location.getLongitude());   // longitude textview
                        et_latitude.setText(""+location.getLatitude());     //latitude textview

                        locationManager.removeUpdates((LocationListener) location);
                        locationManager = null;

                    }else{
                        toastkeun("Terjadi Kesalahan dalam pengambilan koordinat");             //toast function
                        error_text.setText("Terjadi Kesalahan dalam pengambilan koordinat");    //textview for showing errors
                    }
                }catch(Exception e){
                    toastkeun(e.toString());
                }

            }
        });

有什么解决方案吗?

我已经设置了清单文件仅供参考

Hello there i'm having a problem here

i have an add and edit form for my point of views databases
it has two EditText for the coordinates, for Longitude and Latitude
and it has a button to get the current coordinates
if i click the button, the two EditTexts filled with the coordinates
and the user can proceed to insert or edit the data

my code returning a java.nullPointerException, i don't know why...

this is my code :

btn_getkoor.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                try{

                    LocationListener gpsLocation = new MyLocationListener();
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, gpsLocation); //new MyLocationListener());
                    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                    final TextView error_text = (TextView) findViewById(R.id.error_text);

                    if (location != null) {

                        EditText et_longitude = (EditText) findViewById(R.id.et_longitude);
                        EditText et_latitude = (EditText) findViewById(R.id.et_latitude);

                        et_longitude.setText(""+location.getLongitude());   // longitude textview
                        et_latitude.setText(""+location.getLatitude());     //latitude textview

                        locationManager.removeUpdates((LocationListener) location);
                        locationManager = null;

                    }else{
                        toastkeun("Terjadi Kesalahan dalam pengambilan koordinat");             //toast function
                        error_text.setText("Terjadi Kesalahan dalam pengambilan koordinat");    //textview for showing errors
                    }
                }catch(Exception e){
                    toastkeun(e.toString());
                }

            }
        });

Any solution?

I'm already set the Manifest file FYI

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

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

发布评论

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

评论(2

岁吢 2024-11-25 08:04:06

不过,从你的代码中很难说:

如果你还没有这样做,这里是代码:

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);     

It is difficult to say from your code, though :

If you haven't done this, here is the code :

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);     
痴情 2024-11-25 08:04:06
     LocationManager locationManager;
    String bestProvider;
    String LOCATION_SERVICE="location" ; 
    locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        //criteria.setAccuracy();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);  // More accurate, GPS fix.
        bestProvider = locationManager.getBestProvider(criteria,true);
    location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {

                        EditText et_longitude = (EditText) findViewById(R.id.et_longitude);
                        EditText et_latitude = (EditText) findViewById(R.id.et_latitude);

                        et_longitude.setText(""+location.getLongitude());   // longitude textview
                        et_latitude.setText(""+location.getLatitude());     //latitude textview

                       // locationManager.removeUpdates((LocationListener) location);
                        locationManager = null;

                    }else{
                        toastkeun("Terjadi Kesalahan dalam pengambilan koordinat");             //toast function
                        error_text.setText("Terjadi Kesalahan dalam pengambilan koordinat");    //textview for showing errors
                    }
     LocationManager locationManager;
    String bestProvider;
    String LOCATION_SERVICE="location" ; 
    locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        //criteria.setAccuracy();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);  // More accurate, GPS fix.
        bestProvider = locationManager.getBestProvider(criteria,true);
    location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {

                        EditText et_longitude = (EditText) findViewById(R.id.et_longitude);
                        EditText et_latitude = (EditText) findViewById(R.id.et_latitude);

                        et_longitude.setText(""+location.getLongitude());   // longitude textview
                        et_latitude.setText(""+location.getLatitude());     //latitude textview

                       // locationManager.removeUpdates((LocationListener) location);
                        locationManager = null;

                    }else{
                        toastkeun("Terjadi Kesalahan dalam pengambilan koordinat");             //toast function
                        error_text.setText("Terjadi Kesalahan dalam pengambilan koordinat");    //textview for showing errors
                    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文