Android点击Button按钮切换按钮文字显示
本人在使用博客里的程序时出了一点问题:app停止工作,发现原因如下
在activity的onCreate之外用findViewById():
private Button direct=(Button) findViewById(R.id.dir);
public void dir(View v){//单击方向按钮,改变按钮显示的方向(‘F’,‘B’)
if(!dir){
direct.setText(“F”);
sendCommandBinder.sendCommand(“forward”);
dir=true;
}else {
direct.setText(“B”);
sendCommandBinder.sendCommand(“back”);
dir= false;
}
}
应改为:
protected void onCreate(Bundle savedInstanceState) {
direct= (Button) findViewById(R.id.dir);}
private Button direct;
public void dir(View v){//单击方向按钮,改变按钮显示的方向(‘F’,‘B’)
if(!dir){
direct.setText("F");
sendCommandBinder.sendCommand("forward");
dir=true;
}else {
direct.setText("B");
sendCommandBinder.sendCommand("back");
dir= false;
}
}