2017-11-23 1 views
1

나는 갇혀 있고 다른 것들을 시도했지만, 여전히 빈 몸체 오류를 얻기 위해 내가 코드에서 잘못하고있는 것이 무엇인지 알지 못합니다.경고 - 'if'문에 빈 몸체가 있습니다.

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"); 
} 
+0

이 (';') 세미콜론을 제거의 라인'에서 if (buttonText == eatCookie); {' – shash678

+0

다음에 세미콜론을 제거 if (buttonText == eatCookie) –

+0

정말 고마워요 –

답변

1

이 살인 오타 :

if (buttonText == eatCookie);{ 

는 그런데

이 eatCookie 문자열입니다 ... 그래서 실제로 foo를 실행지고 상관없이
//empty if 
if (buttonText == eatCookie); 

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

과 동일 문자열을 비교해서는 안됩니다. ==

+0

그래서 당신은 == –

+0

대신 equals()를 사용해야합니다. 곧 equals()를 사용했습니다. –

+0

'버튼 버튼 = (버튼) findViewById (R.id.button_Option); 최종 문자열 buttonText = button.getText(). toString(); String eatCookie = "EAT COOKIE"; if (buttonText.equals (eatCookie)) {' –

0

중복 ";" 여기

if (buttonText == eatCookie);{ 

제거하십시오.

1

0을 제거해야합니다.세미콜론 (eatCookie); 이후)!

귀하의 코드 :

if (buttonText == eatCookie);{ 

올바른 코드 :

if (buttonText == eatCookie) { 
+0

완료되었지만 처음 클릭 할 때 else 문으로 점프하고 다시 클릭하면 제대로 작동합니다. –

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"); 
} 
관련 문제