我正在尝试连接到服务器并打印响应。
奇怪的是,当我单击活动中启动连接的按钮时,
它会立即强制关闭。
查看后logcat
,我看到的是 VM 正在关闭。
我确实看到似乎有人看到了和我类似的问题:
Android 中的 logcat 显示只是关闭了 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);
}
}
}
任何帮助或建议将不胜感激!谢谢!