Android 不幸的是,Msger已经停止了。致命异常:主
可能重复: 当我尝试在第一个活动上从侦听器运行第二个活动时,main.xml有一个致命的异常。当我取出runTcpClient时;在TcpClient线程上,它可以正常加载 我遇到使用aSyncTask管理UI线程: TcpClientJava.java的代码 } LOGCAT 您正试图在主线程上执行可能较慢的网络操作,这在Android 3.0+中成为一个致命的异常。只需使用AsyncTask或Loader将runTcpClient移动到新线程 下面是一个例子: 试试这个:Android 不幸的是,Msger已经停止了。致命异常:主,android,multithreading,user-interface,Android,Multithreading,User Interface,可能重复: 当我尝试在第一个活动上从侦听器运行第二个活动时,main.xml有一个致命的异常。当我取出runTcpClient时;在TcpClient线程上,它可以正常加载 我遇到使用aSyncTask管理UI线程: TcpClientJava.java的代码 } LOGCAT 您正试图在主线程上执行可能较慢的网络操作,这在Android 3.0+中成为一个致命的异常。只需使用AsyncTask或Loader将runTcpClient移动到新线程 下面是一个例子: 试试这个: class Tc
class TcpClientTask extends AsyncTask<Void, Void, Void> {
private static final int TCP_SERVER_PORT = 1234;
private boolean error = false;
protected Void doInBackground(Void... arg0) {
try {
Socket s = new Socket("10.0.2.2", TCP_SERVER_PORT);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//send output msg
String outMsg = "TCP connecting to " + TCP_SERVER_PORT + System.getProperty("line.separator");
out.write(outMsg);
out.flush();
Log.i("TcpClient", "sent: " + outMsg);
//accept server response
String inMsg = in.readLine() + System.getProperty("line.separator");
Log.i("TcpClient", "received: " + inMsg);
//close connection
s.close();
} catch (UnknownHostException e) {
error = true;
e.printStackTrace();
} catch (IOException e) {
error = true;
e.printStackTrace();
}
return null;
}
protected void onPostExecute() {
if(error) {
// Something bad happened
}
else {
// Success
}
}
}
StringBuilder在将字符串添加到一起时会产生较少的开销,只需在需要获取整个消息时使用msg.toString即可。您不应该从主线程接触网络。 为网络操作实现异步任务
private class MyInnerClass extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String params) {
return "Done";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
调用新的MyInnerClass.execute;从您的主要活动和android将自动调用onPreExecute,在您的网络访问之前,在该方法中执行您想执行的任何操作
Android随后将调用doInBackground
您是否在doInBackground中创建了与网络相关的内容,完成后将自动调用onPostExecute,结果将作为参数传递给此方法 试着使用一小段代码。但这不是好的做法
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectNetwork()
.permitNetwork() //permit Network access
.build());
您可以使用AsynTask或任何线程概念。
我看到了那篇文章,我试着按照他们的建议去做,但没有成功?我对Android还是个新手,我该如何在代码中实现aSyncTask呢?我也应该把所有的活动都改成那样吗?下班后我会试试,让你知道。终于让这项工作开始了,真是太激动了!山姆,你太棒了!我还没有测试过这个。因此,如果它不起作用,请让我知道,我将自己实现并修复它。当我使用此代码时,我得到描述资源路径位置类型TcpClient.TcpClientTask类型必须实现继承的抽象方法AsyncTask.doInBackgroundVoid…TcpClient.java/mesger/src/com/mesger line 32 java ProblemUse protected Void doInBackgroundVoid。。。arg0{并将returnnull;添加到doInBackground的末尾。嘿,山姆。可以在这里使用你的巧妙方法>>
class TcpClientTask extends AsyncTask<Void, Void, Void> {
private static final int TCP_SERVER_PORT = 1234;
private boolean error = false;
protected Void doInBackground(Void... arg0) {
try {
Socket s = new Socket("10.0.2.2", TCP_SERVER_PORT);
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
//send output msg
String outMsg = "TCP connecting to " + TCP_SERVER_PORT + System.getProperty("line.separator");
out.write(outMsg);
out.flush();
Log.i("TcpClient", "sent: " + outMsg);
//accept server response
String inMsg = in.readLine() + System.getProperty("line.separator");
Log.i("TcpClient", "received: " + inMsg);
//close connection
s.close();
} catch (UnknownHostException e) {
error = true;
e.printStackTrace();
} catch (IOException e) {
error = true;
e.printStackTrace();
}
return null;
}
protected void onPostExecute() {
if(error) {
// Something bad happened
}
else {
// Success
}
}
}
StringBuilder msg = new StringBuilder();
String line;
while((line = in.readLine()) != null) // Keep reading until the end of the file is reached
msg.append(in.readLine()).append(System.getProperty("line.separator"));
private class MyInnerClass extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String params) {
return "Done";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectNetwork()
.permitNetwork() //permit Network access
.build());