Logcat显示“关闭VM" [英] Logcat showing "Shutting down VM"

查看:123
本文介绍了Logcat显示“关闭VM"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到服务器并打印响应.
奇怪的是,当我在活动中单击启动连接的按钮时,
它会立即关闭.
查看logcat后,我看到的是VM正在关闭.
我确实看到似乎有人看到了与我类似的问题:
Android中的logcat只是简单地关闭了VM?
不幸的是,我认为问题并没有得到真正解决,这真的使我感到困惑.

I'm trying to connect to a server and print the responses.
Weird thing is that when I click on the button in my activity that starts the connection,
it force closes immediately.
After looking in logcat, what I see is that VM is shutting down.
I do see that there appears to be someone that saw a similar problem as me:
The logcat in android shows simply shutting down the VM?
Unfortunately, I don't think the problem was actually answered, and it really puzzles me.

堆栈跟踪:

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的代码:

Line 3 ("I'm at quote viewer") is the part where I'm in the QuoteViewerActivity where I have a button that connects to the server when pressed.
Code for the QuoteViewerActivity below:

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);
        }
    }
}

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

Any help or suggestion would be much appreciated! Thanks!

推荐答案

我不确定为什么模拟器显示此 exact 错误,但我确实知道您在做什么在4.0之前的设备(和仿真器)中,人们对此并不满意,在4.0+的设备中通常被禁止使用.也就是说,您应该始终在单独的线程上进行任何网络调用.正确的方法是使用 AsyncTask .

I cannot be certain why the emulator is showing this exact error, but I do know for a fact that what you're doing is frowned upon greatly in pre-4.0 devices (and emulators) and typically forbidden in 4.0+ devices. That is, you should always do any network calls on a separate thread. The proper way to do this is with an AsyncTask.

这些对象非常丑陋,但是也是确保您不会占用UI线程的最佳方法. UI线程是您的showQuotes方法当前正在其上运行的线程,Android希望该线程仅做简短的事情.如果开始排队太多事件,则Android 4.0+将使应用程序崩溃.我认为4.0之前的版本似乎只是冻结了(基本上我猜正是您想要的),但是我不会指望它.

These objects are quite ugly, but are also the best way to ensure that you are not tying up the UI thread. The UI thread is the thread on which your showQuotes method is currently running, and Android expects this thread to do only brief things. If it starts to queue up too many events, Android 4.0+ will crash the app. I think pre-4.0 it will just appear to be frozen (essentially exactly what you want it to do I guess), but I wouldn't count on it.

因此,使用AsyncTask,您将这样编写它:

So, with an AsyncTask, you would write it like so:

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线程始终必须或多或少可用于传入事件的原因.

It sucks that you have to do things like this, since it's counter-intuitive compared to writing a Desktop application, but the issue is that Android has to run multiple apps at the same time and be able to quickly suspend your application at any moment when another app comes to the foreground (this allows Android to 'multitask' without the battery draining), which is why your UI thread always has to be more or less available for incoming events.

这篇关于Logcat显示“关闭VM"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆