我有一个问题。当我运行 Android 程序时,出现错误:“不幸的是,Android 已停止”。为什么我在运行应用程序时看到此错误?听到是我的文件:
enter code here
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SimpleClientActivityActivity extends Activity {
private Socket client;
private PrintWriter printwriter;
private EditText textField;
private Button button;
private String messsage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//textField = (EditText) findViewById(R.id.editText1); //reference to the text field
button = (Button) findViewById(R.id.button1); //reference to the send button
//Button press event listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//messsage = textField.getText().toString(); //get the text message on the text field
//textField.setText(""); //Reset the text field to blank
try {
client = new Socket("10.0.2.2", 4444); //connect to server
printwriter = new PrintWriter(client.getOutputStream(),true);
printwriter.write(messsage); //write the message to output stream
printwriter.flush();
printwriter.close();
client.close(); //closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
我想使用我的 adnroid 客户端向 PC 服务器发送短信
最佳答案
可能是因为你在主线程上进行网络操作。查看您的 logcat,会出现红色信息。
关于java - 不幸的是安卓已经停止了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12097596/