为什么我吧监听线程放在了子线程,软件在android4.2还是不能运行?

msnroot 发布于 2013/05/07 10:28
阅读 1K+
收藏 0

我写了个即时通讯软件,想在4.2上运行,4.2的改变时不能把一些费时的功能放在主线程,我就写了个子线程,可是还是不能运行,我贴出主要代码,请大家帮我看看(这个是可以在2.3上运行)


public class ConnectMethod { //绑定socket,DataInputStream DataOutputStream  Socket socket = null;
	DataInputStream in;
	DataOutputStream out;
	ClientThread client;
	Context context;

	public ConnectMethod(Context context) {
		this.context=context;
	}

	public boolean connect() {
		try {
			socket = new Socket(Constants.SERVERIP, Constants.PORT);
			
			if (socket.isConnected()) {
				in = new DataInputStream(socket.getInputStream());
				out = new DataOutputStream(socket.getOutputStream());
				Socketutil socketutil=new Socketutil();
				socketutil.setOut(out);
				socketutil.setIn(in);
				client = new ClientThread(context);
				client.start();
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
		return true;

	}

}



public class ClientThread extends Thread{
	private Recivermsg recivermsg;


	public ClientThread(Context context) {
		recivermsg = new Recivermsg(context);
		
	}
	public void run() {
		recivermsg.start();
	}

}

//接收消息的线程
public class Recivermsg extends Thread {
	private DataInputStream in;
	String msg_in;
	public Recivermsg() { 
         @Override
	public void run() {
		try {
			in=Socketutil.getIn();
			while (true) {
				msg_in = in.readUTF();//接收消息	
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

错误日志

05-07 02:47:05.039: D/AndroidRuntime(759): Shutting down VM
05-07 02:47:05.060: W/dalvikvm(759): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-07 02:47:05.089: E/AndroidRuntime(759): FATAL EXCEPTION: main
05-07 02:47:05.089: E/AndroidRuntime(759): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.weixin/com.example.weixin.Weixin}: android.os.NetworkOnMainThreadException
05-07 02:47:05.089: E/AndroidRuntime(759): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at android.os.Looper.loop(Looper.java:137)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at android.app.ActivityThread.main(ActivityThread.java:5041)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at java.lang.reflect.Method.invokeNative(Native Method)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at java.lang.reflect.Method.invoke(Method.java:511)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at dalvik.system.NativeStart.main(Native Method)
05-07 02:47:05.089: E/AndroidRuntime(759): Caused by: android.os.NetworkOnMainThreadException
05-07 02:47:05.089: E/AndroidRuntime(759): 	at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at libcore.io.IoBridge.connect(IoBridge.java:112)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at java.net.Socket.startupSocket(Socket.java:566)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at java.net.Socket.tryAllAddresses(Socket.java:127)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at java.net.Socket.<init>(Socket.java:177)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at java.net.Socket.<init>(Socket.java:149)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at com.example.Socketconnect.ConnectMethod.connect(ConnectMethod.java:26)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at com.example.weixin.Weixin.onCreate(Weixin.java:58)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at android.app.Activity.performCreate(Activity.java:5104)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-07 02:47:05.089: E/AndroidRuntime(759): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-07 02:47:05.089: E/AndroidRuntime(759): 	... 11 more


加载中
返回顶部
顶部