Android 当运行将在onPostExecute中更新UI的AsyncTask时,操作UI将导致ANR

Android 当运行将在onPostExecute中更新UI的AsyncTask时,操作UI将导致ANR,android,android-asynctask,android-anr-dialog,Android,Android Asynctask,Android Anr Dialog,这是我的密码: AsyncTCPReqTaskCommon task = new AsyncTCPReqTaskCommon(ip, port, new TCPReqCallBackCommon() { @Override public void onReceiveSuccess(String result{ tvText.setText(result); } @Override public void onReceiveFailure() {

这是我的密码:

AsyncTCPReqTaskCommon task = new AsyncTCPReqTaskCommon(ip, port, new 
TCPReqCallBackCommon() {
   @Override
   public void onReceiveSuccess(String result{
       tvText.setText(result);
   }

   @Override
   public void onReceiveFailure() {
   }
});

task.execute(sendCmd);
onReceiveSuccess
将在
onPostExecute
中调用。异步网络请求需要3~4秒。异步任务运行时,滑动UI将导致ANR。当我注释掉这一行时
tvText.setText(结果),则不会显示ANR

异步任务没有在UI线程中运行,为什么会出现ANR?更新
TextView
的正确方法是什么?提前谢谢

我的UI不支持幻灯片。XML:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.mobile.mytestapp.MainActivity">

<TextView
    android:id="@+id/tvText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:layout_gravity="center_horizontal"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="ReqNet"
    android:onClick="ReqNet"/>

</LinearLayout>

谢谢大家。我想我已经找到了原因。在我的AsyncTCPReqTaskCommon中,我从堆中新建了一个1M缓冲区来接收网络响应。在网络操作之后,GC将锁定变量tvText。此GC花费的时间太长,无法执行“tvText.setText(结果);”。于是,一个ANR出现了!我已尝试减小缓冲区大小,ANR不会出现

滑动后的UI是否取决于从AsyncTask接收的数据?如果是这样,这可能是一个问题。请发布错误日志。请发布您的日志。我的UI不支持幻灯片,它是一个简单的xml。您能否尝试将runOnUIThread放在ReceiveSuccess中。runOnUIThread不起作用
03-05 14:33:47.108 12255-12266/com.mobile.mytestapp W/art: Suspending all threads took: 7.100ms
03-05 14:33:47.111 12255-12266/com.mobile.mytestapp I/art: Background sticky concurrent mark sweep GC freed 6(384B) AllocSpace objects, 0(0B) LOS objects, 0% free, 14MB/14MB, paused 9.179ms total 34.364ms
03-05 14:33:59.773 12255-12255/com.mobile.mytestapp I/Choreographer: Skipped 762 frames!  The application may be doing too much work on its main thread.