我的代码中的语法错误,Android应用程序ping IP

时间:2014-01-15 10:11:59

标签: android syntax-error ping curly-braces

我有我的App的Andoid Code我有一点问题, 我不明白为什么会有两个错误,

in line:button.setOnclickListener我在这一行有多个标记      - 语法错误,插入“)”完成      的MethodInvocation      - 语法错误,插入“;”完成声明       - 语法错误,插入“}”以完成ClassBody 和

in Curly brace(在结束代码中)令牌“}”上的语法错误,ConstructorHeaderName无效 我该怎么办 亲切的问候,谢谢 这是我的代码:

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
)
;   
}

1 个答案:

答案 0 :(得分:1)

您应该导入R课程,即:

import com.example.ping.R;

还要确保所有大括号都已正确关闭,最后:

整个代码块:

  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) {: - )

并且最后提示,不要尝试在GUI线程上进行网络操作,你会得到异常,把它放在AsyncTask中