这是我的代码。这里有一些错误,我无法解决它。任何人都可以帮助我吗? view.OnClickListener和View v。如果我使用快速修复来修复错误,则会出现另一个错误。请帮助我:'(
package com.android;
import java.io.BufferedReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.view.View.*;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.text.Editable;
public class Net extends Activity implements View.OnClickListener
{
EditText edt;
TextView text;
Button ping;
@Override
protected void onCreate(Bundle savedInstanceState) //To do auto generated method
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt=(EditText)findViewById(R.id.edt);
text=(TextView)findViewById(R.id.text);
ping=(Button)findViewById(R.id.ping);
Button.setOnClickListener(this);
}
public void onClick(View v) //To do auto generated method
{
Editable host=edt.getText();
}
InetAddress addr=null;
{
Object host;
try
{
addr=InetAddress.getByName(host.toString());
}
catch(UnknownHostException e) //To do 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());
}
try
{
String pingCmd="ping -c5"+host;
String pingResult="";
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String inputLine;
while((inputLine = in.readLine())!=null)
{
System.out.println(inputLine);
text.setText(inputLine+"\n\n");
pingResult += inputLine;
text.setText(pingResult);
}
in.close();
}
catch(IOException e)
{
System.out.println(e);
}
TextView tv = new TextView(this);
tv.setText("\t\t\t\t** Network Tracer ** \n\n\t\tWelcome to Android Network Tracer!");
setContentView(tv);
}
}
答案 0 :(得分:0)
如果错误是编译时间,则很可能在线:
public void onClick(View v) //To do auto generated method
{
Editable host=edt.getText();
}
InetAddress addr=null;
你有封闭的方法定义只有声明,如果你想将所有下面的代码放入onClick方法,请按照:
public void onClick(View v) //To do auto generated method
{
Editable host=edt.getText();
InetAddress addr=null;
Object host;
try
{
addr=InetAddress.getByName(host.toString());
}
catch(UnknownHostException e) //To do 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());
}
try
{
String pingCmd="ping -c5"+host;
String pingResult="";
Runtime r = Runtime.getRuntime();
Process p = r.exec(pingCmd);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String inputLine;
while((inputLine = in.readLine())!=null)
{
System.out.println(inputLine);
text.setText(inputLine+"\n\n");
pingResult += inputLine;
text.setText(pingResult);
}
in.close();
}
catch(IOException e)
{
System.out.println(e);
}
TextView tv = new TextView(this);
tv.setText("\t\t\t\t** Network Tracer ** \n\n\t\tWelcome to Android Network Tracer!");
setContentView(tv);
}
答案 1 :(得分:0)
如何更换此代码:
protected void onCreate(Bundle savedInstanceState) //To do auto generated method
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt=(EditText)findViewById(R.id.edt);
text=(TextView)findViewById(R.id.text);
ping=(Button)findViewById(R.id.ping);
Button.setOnClickListener(this);
}
用这个:
protected void onCreate(Bundle savedInstanceState) //To do auto generated method
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt=(EditText)findViewById(R.id.edt);
text=(TextView)findViewById(R.id.text);
ping=(Button)findViewById(R.id.ping);
ping.setOnClickListener(this);
}
让我们知道这是否有效。 如果没有,那么请不要投票给这个答案:)