gpt4 book ai didi

android - Logcat 显示 "Shutting down VM"

转载 作者:太空宇宙 更新时间:2023-11-03 10:23:58 25 4
gpt4 key购买 nike

我正在尝试连接到服务器并打印响应。
奇怪的是,当我点击 Activity 中启动连接的按钮时,
它会立即强制关闭。
查看 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 行(“I'm at quote viewer”)是我在 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.0 之前的设备(和模拟器)中是非常不受欢迎的,并且通常在 4.0+ 设备中被禁止。也就是说,您应该始终在单独的线程上进行任何网络调用。正确的方法是使用 AsyncTask .

这些对象非常丑陋,但也是确保您不会占用 UI 线程的最佳方式。 UI 线程是您的 showQuotes 方法当前正在运行的线程,Android 希望该线程只做一些简短的事情。如果它开始排队过多的事件,Android 4.0+ 将使应用程序崩溃。我认为 pre-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 线程必须或多或少地对传入事件可用的原因。

关于android - Logcat 显示 "Shutting down VM",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15257976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com