- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试连接到服务器并打印响应。
奇怪的是,当我点击 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/
有些更新会放置一个小盾牌,指示计算机需要重新启动。那个盾牌是注册表项吗?我认为这是关键: HKLM\System\CurrentControlSet\Control\Session Manager\P
我厌倦了启动主管,但出现错误。有人可以帮忙吗?谢谢 /etc/init.d/supervisord 文件。 SUPERVISORD=/usr/local/bin/supervisord SUPERVI
我最近经常遇到这个问题,但我找不到任何解决方案。 我经常收到 Shutting down VM 并且 logcat 没有显示此问题的任何痕迹。 06-08 20:34:25.288 20416-20
我的 android studio 中的模拟器一直显示“正在关闭”。我尝试关闭它并再次运行,重新启动我的 mac,但没有任何效果,每次运行它时,它再次显示“正在关闭”。即使我可以看到下面运行的 Act
我有一个简单的 4 台服务器设置,运行 jMeter(3 个从站,1 个主站): 从站 1:10.135.62.18 运行 ./jmeter-server -Djava.rmi.server.host
我目前正在使用 JavaFX 开发 Java 应用程序。来自程序的窗口将作为低分辨率 LED 屏幕上的流。我需要的是完全关闭窗口内所有文本(标签文本、文本区域等)的抗锯齿功能。 我用谷歌搜索了几次,尝
MyApplication 静默终止。它打印的最后一件事是来自某个函数的跟踪日志,然后 Vm 关闭而不引用我的应用程序。 这是日志 fragment 05-17 15:25:28.294 4385
我已经创建了无障碍服务,我想调用 OnAccessibilityEvent()仅当“关机”菜单出现时,我的目标是调用 TTS 引擎,以便仅当屏幕上出现此菜单时才让手机通话。 我需要了解的是如何检测这唯
嘿,所以我正在制作一个序列化函数,它采用基类指针 'Joint',提取 union 后代的 'type',然后 想要实例化正确类型的“定义”,基于指针实际指向的任何类型的“关节”。 但是我仍然收到关于
我正在尝试连接到服务器并打印响应。 奇怪的是,当我点击 Activity 中启动连接的按钮时, 它会立即强制关闭。 查看 logcat 后,我看到 VM 正在关闭。 我确实看到似乎有人看到了与我类似的
我想知道 - 当我写了一段代码,最多可以更新 10,000 行并且可能需要几秒钟才能完成时,如果通过 ajax 请求访问文件时,post 查询是否发送到 php 文件,然后浏览器关闭,文件是否得到完全
是的,还有其他问题非常相似。但是,此错误的原因似乎有所不同。 我启动了一次 MongoDB,它在我指定的数据文件夹中运行良好。当我再次运行它时(有时),它突然不再工作并给我以下输出日志: 2018-0
由于某种原因,LogCat 不再在崩溃时报告任何堆栈跟踪。相反,我只得到以下信息: D/AndroidRuntime: Shutting down VM W/dalvikvm: threadid=1:
当在超过一定数据大小(~2,5gb)的集群上运行sparkJob时,我收到“作业被取消,因为SparkContext被关闭”或“执行程序丢失”。当查看yarn gui时,我发现被杀死的工作是成功的。运
我正在尝试使用 this 实现一个服务器(能够接受多个客户端)和一个客户端教程并进行一些扩展。除了一项功能外,一切正常:当服务器运行1分钟后,它就会关闭,每个客户端都必须打印消息“服务器关闭”并关闭。
我正在 Android studio 中编码。我从 gitHub 克隆了一个项目 https://github.com/QuadFlask/colorpicker 但我有这个问题, 我不知道如何解决这
我正在尝试使用 Http 将数据发布到 REST 服务,并且我已按如下方式配置我的客户端: PoolingHttpClientConnectionManager cm = new PoolingHtt
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 10 年前。 Improve thi
我目前正在开发一个 Java DSL 路由,它将从 JMS 队列中获取一条消息,对其进行处理,然后使用 JPA 将其放入数据库中。真的很简单: public void configure() {
在我的客户端 Ant 任务中从 Jetty 服务器下载文件,我偶尔会得到一个 javax.net.ssl.SSLException: SSL peer shut down incorrectly at
我是一名优秀的程序员,十分优秀!