【发布时间】:2012-11-10 15:11:10
【问题描述】:
我想在 EditText 框中输入一个 ip, 然后通过按钮 ping(是否可达)在 textView 中给出结果
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();
}
}
});
}
}
我在真机上按ping键,应用程序就出来了,为什么?
【问题讨论】:
-
如果不分析代码 - 您的应用程序很可能只是崩溃了。查看
logcat输出,看看发生了什么,不要猜测!
标签: java android eclipse ping inetaddress