【问题标题】:Why wont my onClick work?为什么我的 onClick 不起作用?
【发布时间】:2015-06-25 19:20:48
【问题描述】:

似乎我的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) {
        }        
    }
}

【问题讨论】:

    标签: android onclick eventhandler


    【解决方案1】:

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

    必须在setContentView之后调用

    【讨论】:

    • hm... 我刚刚意识到即使在我更改呼叫后问题仍然存在,android 工作室希望我更改 Button refresh = (Button) findViewById(R.id.button);如果我在 onCreate() 中调用它,则为“最终”,并且无论是否将其设为最终版本,文本仍然不会改变,有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 2021-09-30
    相关资源
    最近更新 更多