Why wont my onClick work?

45 views Asked by At

It seems like my onClick is malfunctioning, the text won't change when I click it and the button is still enabled after a click.

I'm trying to get a button that (when pressed by the user) updates the location.

Here's my code:

public class Search extends ActionBarActivity {

    private double[][] values_gps = new double[100][1];
    private Location location;
    private TextView textLat;
    private TextView textlong;
    private Context mContext;
    private double CurrentLatitude = 0;
    private double CurrentLongitude = 0;

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new myLocationListener();
    Button refresh = (Button) findViewById(R.id.button);

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        textLat = (TextView)findViewById(R.id.Lat);
        textlong = (TextView)findViewById(R.id.Long);

        refresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
                refresh.setEnabled(false);
                refresh.setText("SEARCHING");    
            }
        });
    }        

    class myLocationListener implements LocationListener{

        @Override
        public void onLocationChanged(Location location) {

            textLat.setText(Double.toString(location.getLatitude()));
            textlong.setText(Double.toString(location.getLongitude()));

            lm.removeUpdates(ll);
            refresh.setEnabled(true);
            refresh.setText("SEARCH");        
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }        
    }
}
1

There are 1 answers

1
eriuzo On

Button refresh = (Button) findViewById(R.id.button);

must be called after setContentView