4

我正在尝试连接到服务器并打印响应。
奇怪的是,当我单击活动中启动连接的按钮时,
它会立即强制关闭。
查看后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);
        }
    }
}

任何帮助或建议将不胜感激!谢谢!

4

1 回答 1

2

我不能确定为什么模拟器会显示这个确切的错误,但我确实知道你正在做的事情在 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 线程总是或多或少地可用于传入事件的原因。

于 2013-03-06T21:16:58.557 回答