1

I want to input an ip in an EditText box, then give the result in a textView via button ping ( reachable or not)

public class MainActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
            EditText editText = (EditText) findViewById(R.id.editText1);
            TextView textView = (TextView) findViewById(R.id.textView);
             String input= editText.getText().toString();

               InetAddress ip;
                ip = InetAddress.getByName(input);
                boolean reach= ip.isReachable(5000);
                   textView.setText("real ?"+reach);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        }
    });
}   
}

Whenever i press the ping button in the real phone, it comes out of the applicaton, why?

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
user1814662
  • 281
  • 1
  • 3
  • 5
  • 3
    Without analyzing the code - most likely your application is simply crashing. Look into `logcat` output to see what's going on, don't guess! – Code Painters Nov 10 '12 at 15:14

2 Answers2

0

I guess you are wrong with the net method, try this code and if it is still unworkable please tell me.

public class MainActivity extends Activity {
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            try {
            EditText editText = (EditText) findViewById(R.id.editText1);
            final TextView textView = (TextView) findViewById(R.id.textView);
             final String input= editText.getText().toString();
             new Thread(new Runnable(){
                 InetAddress ip;
                ip = InetAddress.getByName(input);
                final boolean reach= ip.isReachable(5000);
                runOnUiThread(new Runnable() {
                    textView.setText("real ?"+reach);
                });
             }).start(); 



                   textView.setText("real ?"+reach);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   
        }
    });
}   
}  
Cenxui
  • 1,343
  • 1
  • 17
  • 25
-1

Idk if you are looking for an answer to this still but the reason your app is crashing is because you are not allowed to perform a network operation e.g. ip = InetAddress.getByName(input); in the mainactivity please create another class and let it extend AsyncTask and perform your network operations there.

Thanks

frank johnson
  • 153
  • 2
  • 9