Question

I have my App's Andoid Code I have a little problem, I do not understand why there are two Errors,

in line: button.setOnclickListener I have Multiple markers at this line - Syntax error, insert ")" to complete MethodInvocation - Syntax error, insert ";" to complete Statement - Syntax error, insert "}" to complete ClassBody and

in Curly brace (in end code) Syntax error on token "}", invalid ConstructorHeaderName what should i do kind regards, Thanks this is my code:

package com.example.ping;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

EditText edit;
TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    edit = (EditText)findViewById(R.id.editText1);
    edit.setText("192.xxx.x.x");
    text = (TextView)findViewById(R.id.textView1);

    pingIP();

}

private void pingIP() {
Button button = (Button)findViewById(R.id.button1); // HERE ERROR
     /**Multiple markers at this line
- Syntax error, insert ")" to complete 
 MethodInvocation
- Syntax error, insert ";" to complete Statement
 - Syntax error, insert "}" to complete ClassBody **/

    button.setOnClickListener(new View.OnClickListener() {


            try {
                Object host;
                addr = InetAddress.getByName(host.toString());
                InetAddress addr = null;
            addr = InetAddress.getByName(host.toString());
            } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            try {
            if(addr.isReachable(5000)) {
            text.append("\n" + host + " - Respond OK");
            } else {
            text.append("\n" + host);
            }
            } catch (IOException e) {

            text.append("\n" + e.toString());
            }
            }
        public void onClick(View v) {

            Editable host = edit.getText();
        }
    } //HERE ERROR - Syntax error on token "}", invalid ConstructorHeaderName
)
;   
}
Was it helpful?

Solution

You should import your R class, ie.:

import com.example.ping.R;

also make sure all your braces are properly closed, and finally:

whole your code block:

  try {
                Object host;
                addr = InetAddress.getByName(host.toString());
                InetAddress addr = null;
            addr = InetAddress.getByName(host.toString());
            } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            try {
            if(addr.isReachable(5000)) {
            text.append("\n" + host + " - Respond OK");
            } else {
            text.append("\n" + host);
            }
            } catch (IOException e) {

            text.append("\n" + e.toString());
            }
            }

is outside your method public void onClick(View v) { :-)

and last hint, dont try do network operations on GUI thread, you will get exceptions, put it inside AsyncTask

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top