-1

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

Falmesino
  • 135
  • 1
  • 5
  • 16

2 Answers2

0

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);     
Reno
  • 33,594
  • 11
  • 89
  • 102
0
     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
                    }
Rasel
  • 15,499
  • 6
  • 40
  • 50
  • Sorry but this is wrong : `locationManager.removeUpdates((LocationListener) location)` – Reno Jul 04 '11 at 04:37