我正在尝试连接服务器并打印回复
奇怪的是,当我点击我的活动中的按钮开始连接时,
它会立即关闭。
查看logcat
后,我看到VM正在关闭
我确实看到有人像我一样看到了类似的问题:
The logcat in android shows simply shutting down the VM?
不幸的是,我认为这个问题实际上并没有得到解答,这让我很困惑。
堆栈追踪:
03-06 20:12:47.012: I/System.out(2757): I'm in main
03-06 20:12:48.092: D/gralloc_goldfish(2757): Emulator without GPU emulation detected.
03-06 20:13:03.462: I/System.out(2757): I'm at quote viewer
03-06 20:13:04.291: I/Choreographer(2757): Skipped 32 frames! The application may be doing too much work on its main thread.
03-06 20:13:09.881: D/AndroidRuntime(2757): Shutting down VM
03-06 20:13:09.881: W/dalvikvm(2757): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-06 20:13:09.901: D/dalvikvm(2757): GC_CONCURRENT freed 130K, 9% free 2652K/2888K, paused 80ms+5ms, total 222ms
03-06 20:13:09.941: E/AndroidRuntime(2757): FATAL EXCEPTION: main
03-06 20:13:09.941: E/AndroidRuntime(2757): java.lang.IllegalStateException: Could not execute method of the activity
第3行(“我在引用查看器”)是我在QuoteViewerActivity中的一个部分,我按下了一个连接到服务器的按钮。
下面QuoteViewerActivity的代码:
package com.pheno.networkprogrammingiphenoexercise;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class QuoteViewerActivity extends Activity {
private String mHost = "ota.iambic.com";
private int mPort = 17;
private TextView mQuote1, mQuote2, mQuote3;
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println("I'm at quote viewer");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote_viewer);
mQuote1 = (TextView)findViewById(R.id.quote1);
mQuote2 = (TextView)findViewById(R.id.quote2);
mQuote3 = (TextView)findViewById(R.id.quote3);
}
public void showQuotes(View clickedButton) {
try {
TextView[] quoteArray = {mQuote1, mQuote2, mQuote3};
for(int i = 0; i < 3; i++) {
Socket socket = new Socket(mHost, mPort);
System.out.println("Get Socket");
BufferedReader in = SocketUtils.getReader(socket);
String quoteResult = in.readLine();
System.out.println(quoteResult);
quoteArray[i].setText(quoteResult);
socket.close();
}
} catch(UnknownHostException uhe) {
mQuote1.setText("Unknown host: " + mHost);
//uhe.printStackTrace();
Log.e("pheno", "What happened", uhe);
} catch(IOException ioe) {
mQuote1.setText("IOException: " + ioe);
Log.e("pheno", "What happened", ioe);
}
}
}
非常感谢任何帮助或建议!谢谢!
答案 0 :(得分:1)
我无法确定为什么模拟器会显示此完全错误,但我确实知道您正在做的事情在4.0之前的设备(和模拟器)中非常不受欢迎通常禁止在4.0+设备中使用。也就是说,您应该始终在单独的线程上进行任何网络调用。正确的方法是使用AsyncTask。
这些对象非常难看,但也是确保您不会占用UI线程的最佳方法。 UI线程是您的showQuotes
方法当前正在运行的线程,Android期望此线程只执行简短的操作。如果它开始排队太多事件,Android 4.0+将使应用程序崩溃。我认为4.0之前它似乎会被冻结(基本上就是我想要的那样),但我不会指望它。
所以,使用AsyncTask,你会这样写:
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>()
{
String someData = "";
@Override
protected Void doInBackground(Void... unused)
{
// Here, do your networking stuff, but don't access
// UI elements (such as whatever.setText).
someData = getStuffFromSocketsBlahBlah();
// This method is called from a different (non-UI thread) automatically
// shortly after you call task.execute()
}
@Override
protected void onPostExecute(Void unused)
{
// This will get called from the main (UI thread) shortly after doInBackground returns.
// Here it is okay to access UI elements but not to do any serious work
// or blocking network calls (such as socket.read, etc.)
someTextView.setText(someData);
}
};
task.execute(null, null, null);
很糟糕,你必须做这样的事情,因为与编写桌面应用程序相比,这是违反直觉的,但问题是Android必须同时运行多个应用程序并能够快速暂停你的应用程序任何时候,当另一个应用程序到达前台时(这允许Android在没有电池耗尽的情况下'多任务'),这就是为什么你的UI线程总是必须或多或少地可用于传入事件。