1

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ボタンを押すたびに、アプリケーションから出てくるのはなぜですか?

4

2 に答える 2

-1

これに対する答えをまだ探しているが、アプリがクラッシュする理由は、ネットワーク操作の実行が許可されていないためです。メインアクティビティで別のクラスを作成し、AsyncTask を拡張してそこでネットワーク操作を実行させてください。

ありがとう

于 2013-03-25T20:32:55.313 に答える