我想测试StackOverFlow中的帖子中的代码,但是当我在模拟器上运行此代码时,一段时间后,应用程序终止并且android给我“不幸的是MyReceiver01已停止”。请帮我。 感谢。
SocketServer.java:
package amiref.project.network;
import android.os.*;
import android.app.*;
import java.io.*;
import java.net.*;
import android.view.*;
import android.widget.*;
import android.util.*;
import android.view.View.*;
public class SocketServer extends Activity {
ServerSocket ss = null;
String mClientMsg = "";
Thread myCommsThread = null;
protected static final int MSG_ID = 0x1337;
public static final int SERVERPORT = 6000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.TextView01);
tv.setText("Nothing from client yet");
this.myCommsThread = new Thread(new CommsThread());
this.myCommsThread.start();
}
@Override
protected void onStop() {
super.onStop();
try {
// make sure you close the socket upon exiting
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Handler myUpdateHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_ID:
TextView tv = (TextView) findViewById(R.id.TextView01);
tv.setText(mClientMsg);
break;
default:
break;
}
super.handleMessage(msg);
}
};
class CommsThread implements Runnable {
public void run() {
Socket s = null;
try {
ss = new ServerSocket(SERVERPORT );
} catch (IOException e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
Message m = new Message();
m.what = MSG_ID;
try {
if (s == null)
s = ss.accept();
BufferedReader input = new BufferedReader(
new InputStreamReader(s.getInputStream()));
String st = null;
st = input.readLine();
mClientMsg = st;
myUpdateHandler.sendMessage(m);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="amiref.project.network"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="amiref.project.network.SocketServer"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Log cat:
06-27 21:32:14.008 37-2170/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
06-27 21:32:14.478 2208-2208/? E/Trace: error opening trace file: No such file or directory (2)
06-27 21:32:14.918 629-629/? E/ActivityThread: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf59d0 that was originally bound here
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf59d0 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
at android.app.ContextImpl.bindService(ContextImpl.java:1418)
at android.app.ContextImpl.bindService(ContextImpl.java:1407)
at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
06-27 21:32:15.068 2208-2221/? E/AndroidRuntime: FATAL EXCEPTION: Thread-150
java.lang.NullPointerException
at amiref.project.network.SocketServer$CommsThread.run(SocketServer.java:67)
at java.lang.Thread.run(Thread.java:856)
06-27 21:32:15.318 629-629/? E/StrictMode: null
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cf59d0 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
at android.app.ContextImpl.bindService(ContextImpl.java:1418)
at android.app.ContextImpl.bindService(ContextImpl.java:1407)
at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
06-27 21:32:15.428 37-87/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
06-27 21:32:16.149 37-87/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
06-27 21:32:16.487 37-87/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
06-27 21:32:16.818 629-629/? E/ActivityThread: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cd9588 that was originally bound here
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cd9588 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
at android.app.ContextImpl.bindService(ContextImpl.java:1418)
at android.app.ContextImpl.bindService(ContextImpl.java:1407)
at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
06-27 21:32:16.838 629-629/? E/StrictMode: null
android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cd9588 that was originally bound here
at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
at android.app.ContextImpl.bindService(ContextImpl.java:1418)
at android.app.ContextImpl.bindService(ContextImpl.java:1407)
at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
at com.android.emailcommon.service.AccountServiceProxy.getDeviceId(AccountServiceProxy.java:116)
at com.android.exchange.ExchangeService.getDeviceId(ExchangeService.java:1249)
at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1856)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)