警告 - '如果'声明有空体

时间:2017-11-23 05:41:05

标签: if-statement

我陷入困境并尝试了不同的事情,但我仍然不知道我在代码中做错了什么来获得空身错误。

public void eatCookie(View view) {
    Button button = (Button) findViewById(R.id.button_Option);
    final String buttonText = button.getText().toString();
    String eatCookie = "EAT COOKIE";
    if (buttonText == eatCookie);{
        // TODO: Find a reference to the ImageView in the layout. Change the image.
        ImageView imageView = (ImageView) findViewById(R.id.android_cookie_image_view);
        imageView.setImageResource(R.drawable.after_cookie);


        // TODO: Find a reference to the TextView in the layout. Change the text.
        TextView textView = (TextView) findViewById(R.id.status_text_view);
        textView.setText("I'm so full");

        button.setText("Reset");
}

4 个答案:

答案 0 :(得分:2)

这个杀人错字:

if (buttonText == eatCookie);{

相同
//empty if 
if (buttonText == eatCookie);

//some code in an inner scope
{
    //foo
}

所以实际上foo无论如何都会被执行......

顺便说一句,eatC​​ookie是一个字符串,所以你永远不应该使用==来比较字符串

答案 1 :(得分:1)

您必须删除;分号(在eatCookie);之后)!

您的代码:

if (buttonText == eatCookie);{

正确的代码:

if (buttonText == eatCookie) {

答案 2 :(得分:0)

你有一个多余的“;”这里

if (buttonText == eatCookie);{

删除它。

答案 3 :(得分:0)

只需清理;,这是错误的语法

public void eatCookie(View view) {

Button button = (Button) findViewById(R.id.button_Option);
final String buttonText = button.getText().toString();
String eatCookie = "EAT COOKIE";
if (buttonText == eatCookie){
    // TODO: Find a reference to the ImageView in the layout. Change the image.
    ImageView imageView = (ImageView) findViewById(R.id.android_cookie_image_view);
    imageView.setImageResource(R.drawable.after_cookie);


    // TODO: Find a reference to the TextView in the layout. Change the text.
    TextView textView = (TextView) findViewById(R.id.status_text_view);
    textView.setText("I'm so full");

    button.setText("Reset");
}