开发者

Android - onClick errors in SMS app

开发者 https://www.devze.com 2023-04-01 02:20 出处:网络
I have a button that sends message - however the onClick feature in the onClickListener seems to give me a few errors when I tell it to disable the button when click, AND when I set toast to it. Oh, a

I have a button that sends message - however the onClick feature in the onClickListener seems to give me a few errors when I tell it to disable the button when click, AND when I set toast to it. Oh, and the error underlines 'else' saying syntax error. (Other than that the code works fine and sends the SMS)

public void onClick(View v) 
{                
    messageinfo mi = new messageinfo();
    String message = txtMessage.getText().toString();                 

    if (message.length()>0)                
        sendSMS(mi.SMSNO()开发者_StackOverflow中文版, smsmessage);  
        Toast.makeText(getBaseContext(), "sending", Toast.LENGTH_SHORT).show();
        myButton.setEnabled(false);

    else
        Toast.makeText(getBaseContext(), "enter your message", Toast.LENGTH_SHORT).show();
}

Is there an easy solution to this?


Through formatting your code I've noticed that it would appear that your if statement is failing.

You need to have { }s around the resulting code from an if statement, eg:

if(true){
  line 1;
  line 2;
  line 3;
}
else {
  line 1;
  line 2;
  line 3;
}

The only time you can miss out the { }s is when you've only got ONE statement following an if. This is always a bad idea however, its not much effort just to put in some { }s, and means that issues like this won't ever happen.

Clarification

The code in your question is running the if and if it matches it runs THE NEXT LINE. Then the code carries on OUTSIDE of the if clause (because of missing brackets)so then will run the next two lines REGARDLESS of the outcome of the if statement.

Then it encounters the else which its not expecting. Its supposed to be after either { .. } or directly after the FIRST statement after the if, so that's why it fails at the else.

0

精彩评论

暂无评论...
验证码 换一张
取 消