程序运行后,单击发送按钮,然后弹出对话框表示程序已经退出。
我不知道是什么问题,我该如何解决? 有没有人有同样的问题,你能帮助我吗?
package com.secion9.clienttest;
import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import java.net.Socket;
import java.io.IOException;
import java.io.OutputStream;
import android.view.View;
import android.view.View.OnClickListener;
public class ClientActivity extends Activity {
Button btSend;
EditText etMsg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
btSend=(Button)this.findViewById(R.id.button1);
etMsg=(EditText)this.findViewById(R.id.EditText1);
btSend.setOnClickListener(
new OnClickListener(){
public void onClick(View v)
{
try
{
Socket socket=new Socket("127.0.0.1",5050);
OutputStream outputStream=socket.getOutputStream();
byte[] buffer=etMsg.getText().toString().getBytes();
outputStream.write(buffer);
outputStream.flush();
}
catch(IOException e)
{
e.printStackTrace();
}
}
});
}
}
logcat的
09-02 08:24:31.189: E/Trace(627): error opening trace file: No such file or directory (2)
09-02 08:24:32.259: D/gralloc_goldfish(627): Emulator without GPU emulation detected.
09-02 08:24:32.598: I/Choreographer(627): Skipped 40 frames! The application may be doing too much work on its main thread.
09-02 08:25:04.471: D/AndroidRuntime(627): Shutting down VM
09-02 08:25:04.471: W/dalvikvm(627): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-02 08:25:04.539: E/AndroidRuntime(627): FATAL EXCEPTION: main
09-02 08:25:04.539: E/AndroidRuntime(627): android.os.NetworkOnMainThreadException
09-02 08:25:04.539: E/AndroidRuntime(627): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-02 08:25:04.539: E/AndroidRuntime(627): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
09-02 08:25:04.539: E/AndroidRuntime(627): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
09-02 08:25:04.539: E/AndroidRuntime(627): at libcore.io.IoBridge.connect(IoBridge.java:112)
09-02 08:25:04.539: E/AndroidRuntime(627): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-02 08:25:04.539: E/AndroidRuntime(627): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
09-02 08:25:04.539: E/AndroidRuntime(627): at java.net.Socket.startupSocket(Socket.java:566)
09-02 08:25:04.539: E/AndroidRuntime(627): at java.net.Socket.tryAllAddresses(Socket.java:127)
09-02 08:25:04.539: E/AndroidRuntime(627): at java.net.Socket.<init>(Socket.java:177)
09-02 08:25:04.539: E/AndroidRuntime(627): at java.net.Socket.<init>(Socket.java:149)
09-02 08:25:04.539: E/AndroidRuntime(627): at com.secion9.clienttest.ClientActivity$1.onClick(ClientActivity.java:31)
09-02 08:25:04.539: E/AndroidRuntime(627): at android.view.View.performClick(View.java:4084)
09-02 08:25:04.539: E/AndroidRuntime(627): at android.view.View$PerformClick.run(View.java:16966)
09-02 08:25:04.539: E/AndroidRuntime(627): at android.os.Handler.handleCallback(Handler.java:615)
09-02 08:25:04.539: E/AndroidRuntime(627): at android.os.Handler.dispatchMessage(Handler.java:92)
09-02 08:25:04.539: E/AndroidRuntime(627): at android.os.Looper.loop(Looper.java:137)
09-02 08:25:04.539: E/AndroidRuntime(627): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-02 08:25:04.539: E/AndroidRuntime(627): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 08:25:04.539: E/AndroidRuntime(627): at java.lang.reflect.Method.invoke(Method.java:511)
09-02 08:25:04.539: E/AndroidRuntime(627): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-02 08:25:04.539: E/AndroidRuntime(627): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-02 08:25:04.539: E/AndroidRuntime(627): at dalvik.system.NativeStart.main(Native Method)
09-02 08:25:07.079: I/Process(627): Sending signal. PID: 627 SIG: 9
答案 0 :(得分:0)
已经放入了AndroidManifest.xml的Internet权限吗?
此致
答案 1 :(得分:0)
Skipped 40 frames! The application may be doing too much work on its main thread.
和
NetworkOnMainThreadException
有没有告诉你你正在阻止主线程,这会导致ANR和应用程序被杀死:)
尝试在后台线程或AsyncTask
上实现您的网络内容答案 2 :(得分:0)
听起来你正在主线程上进行网络连接。这是禁忌:
解决方案:
修改您的应用,以便它产生线程或异步任务来执行网络I / O.
这是一个很好的链接,讨论如何:
答案 3 :(得分:0)
根据[Android开发者] [1]的文档:
应用程序尝试执行时抛出的异常 在其主线程上进行网络操作。
这仅适用于针对Honeycomb SDK或。的应用程序 更高。允许使用早期SDK版本的应用程序 他们的主要事件循环线程上的网络,但它是很重要的 气馁。
因此,您需要定位较低的SDK或创建用于处理网络连接的新线程。