我有以下代码:
public class MainActivity extends Activity {
int a1_val = 0;
int b1_val = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goto_start);
Button a1_btn =(Button)findViewById(R.id.a1_btn);
Button b1_btn =(Button)findViewById(R.id.b1_btn);
Button result_btn =(Button)findViewById(R.id.result_btn);
// a1 click
a1_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
a1_val = 1;
};
});
// b1 click
b1_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
b1_val = 5;;
};
});
result_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if { (a1_val + b1_val == 6);
View startpage = (View)findViewById(R.id.startpage);
startpage.setVisibility(View.VISIBLE);
};
else break;
当我点击result_btn时,为什么应用程序会崩溃?我认为我在if语句中犯了一个错误,但我不知道问题是什么。
答案 0 :(得分:1)
我看到的问题是IF语句的括号,这是修复:
if (a1_val + b1_val == 6) {
View startpage = (View) findViewById(R.id.startpage);
startpage.setVisibility(View.VISIBLE);
}
我认为这不是唯一的问题......
编辑 - 这是重写的整个代码:
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goto_start);
final Button a1_btn = (Button) findViewById(R.id.a1_btn);
final Button b1_btn = (Button) findViewById(R.id.b1_btn);
Button result_btn = (Button) findViewById(R.id.result_btn);
// a1 click
a1_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setText(1);
}
});
// b1 click
b1_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setText(5);
}
});
result_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if ((Integer.parseInt(a1_btn.getText()) + Integer.parseInt(b1_btn.getText())) == 6) {
View startpage = (View) findViewById(R.id.startpage);
startpage.setVisibility(View.VISIBLE);
}
});
}
// ... other code of Activity