为什么我的onClick不工作?

时间:2015-06-25 19:20:49

标签: android onclick eventhandler

好像我的onClick出现故障,单击它时文本不会改变,点击后按钮仍然启用。

我正在尝试获取一个按钮(当用户按下时)更新位置。

这是我的代码:

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 个答案:

答案 0 :(得分:0)

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

必须在setContentView

之后调用