Skip to content Skip to sidebar Skip to footer

How Do I Want To Fix Onclicklistener1

this is my code. there is some error here and i couldn't settle it. can anyone pls help me? there is an error at view.OnClickListener and View v. If i using quick fix to fix an er

Solution 1:

If error is compile time then most probably here on line:

publicvoidonClick(View v) //To do auto generated method  
     {
     Editable host=edt.getText();
     }
    InetAddress addr=null;

you have closed method definition having only statement, if you want to place all the following code into onClick method, follow as:

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

Solution 2:

How about replacing this code:

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

with this:

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

Let know if this works. If not, then please dont down vote this answer :)

Post a Comment for "How Do I Want To Fix Onclicklistener1"