我尝试将方法实现到Button的onClickListener中,并且得到了Syntax Error

下面是我的代码:

// button click event
btnCreateNewEvent.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
// Checking for server respond
if (serverRespond() == false) {
           //DO SOMETHING
}


以下是我在ServerRespond方法之外的onCreate

// Check server respond
public boolean serverRespond() {
boolean success = false;
    try {
        URL url = new URL(url_create_event);
        HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
        urlcon.connect();
        if (urlcon.getResponseCode() == 200) {
            success = true;
        }
    } catch (Exception e) {
        Log.e(create_error.toString(), "Server is not responding");
    }
    return success;
}


错误:if(serverRespond()==true){

"The method serverRespond() is undefined for the type new View.OnClickListener(){}"

最佳答案

您可以通过以下方式访问:

YourActivity.this.serverRespond()

07-24 09:20