【问题标题】:Android - non UI Thread rendering my UI unresponsiveAndroid - 非 UI 线程呈现我的 UI 无响应
【发布时间】:2018-11-11 16:09:57
【问题描述】:

当我在我的活动中调用此线程时,它会使 UI 无响应,因此一旦它启动我就无法执行任何操作。我认为它与 I/O 流有关,但我不确定如何解决这个问题。任何帮助将不胜感激,但我是 Android 和线程的新手,所以我可能有一些后续问题。

这是我的主题:

 mHandler = new Handler();

    new Thread(new Runnable() {
        @Override
        public void run () {
            mHandler.post(new Runnable() {
                @Override
                public void run () {
                    int counter = 0;
                    while (true) {
                        counter++;
                        try {
                            output = "";
                            if (mInStream != null) {
                                mInStream.read(buffer);
                                for (byte b : buffer) {
                                    char c = (char) b;
                                    if (c >= ' ' && c < 'z') {
                                        //System.out.print(c);
                                        output += c;
                                    }
                                }
                                //System.out.println();
                                Intent intent = new Intent();
                                intent.setAction("com.curie.WEIGHT_RECEIVED");
                                intent.putExtra("Output",output);

                                if (counter % 10 == 0) {
                                    System.out.println(counter);
                                    LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcastSync(intent);
                                }
                            }
                            // Send the obtained bytes to the UI Activity
                        } catch (IOException e) {
                            //an exception here marks connection loss
                            //send message to UI Activity
                            break;
                        }
                    }
                }
            });
        }
    }).start();

编辑:答案确实解决了我的问题,但我现在收到此错误。 当我调用 inputwindow.setText(weight) 时发生此错误,它将 TextView 中的文本设置为从线程广播的值。

E/AndroidRuntime: FATAL EXCEPTION: Thread-2358
        Process: com.example.curie.fairbanks_03, PID: 15718
        android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8358)
        at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1364)
        at android.view.ViewGroup.invalidateChild(ViewGroup.java:5433)
        at android.view.View.invalidateInternal(View.java:13997)
        at android.view.View.invalidate(View.java:13961)
        at android.view.View.invalidate(View.java:13945)
        at android.widget.TextView.checkForRelayout(TextView.java:8403)
        at android.widget.TextView.setText(TextView.java:5011)
        at android.widget.TextView.setText(TextView.java:4836)
        at android.widget.TextView.setText(TextView.java:4811)
        at com.example.curie.fairbanks_03.InputActivity$2.onReceive(InputActivity.java:67)
        at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:311)
        at android.support.v4.content.LocalBroadcastManager.sendBroadcastSync(LocalBroadcastManager.java:289)
        at com.example.curie.fairbanks_03.BluetoothConnection$2.run(BluetoothConnection.java:176)
        at java.lang.Thread.run(Thread.java:818)

【问题讨论】:

  • 您是否确认run 确实发生在单独的线程上(例如,使用adb logcat -v threadtime)? new Handler(); 将为您提供与Looper 当前线程相关联的Handler
  • 当我在我的活动中运行线程时,它会将广播打印到控制台,以便我可以看到它正在运行和工作,但之后我无法在屏幕上执行任何操作。
  • 好吧,我不是问你是否验证过Runnable 是否运行过。我在问你是否验证了 哪个线程 它最终运行在哪个线程上。
  • 我不确定如何确定。
  • 一种方法是添加日志打印,然后使用我在第一条评论中提到的adb 命令查看日志来自哪个线程。

标签: java android multithreading inputstream outputstream


【解决方案1】:

它使您的 UI 无响应,因为它在 UI 线程中运行!您启动一个新线程来运行任务,然后在该线程中调用mHandler.post,它只是返回到创建mHandler 的线程,即UI 线程。没有理由这样做。只需在您创建的线程中执行操作,无需调用处理程序。

new Thread(new Runnable() {
    @Override
    public void run () {
        int counter = 0;
        while (true) {
            counter++;
            try {
                output = "";
                if (mInStream != null) {
                    mInStream.read(buffer);
                    for (byte b : buffer) {
                        char c = (char) b;
                        if (c >= ' ' && c < 'z') {
                            //System.out.print(c);
                            output += c;
                        }
                    }
                    //System.out.println();
                    Intent intent = new Intent();
                    intent.setAction("com.curie.WEIGHT_RECEIVED");
                    intent.putExtra("Output",output);

                    if (counter % 10 == 0) {
                        System.out.println(counter);
                        LocalBroadcastManager.getInstance(InputActivity.getContext()).sendBroadcastSync(intent);
                    }
                }
                // Send the obtained bytes to the UI Activity
            } catch (IOException e) {
                //an exception here marks connection loss
                //send message to UI Activity
                break;
            }
        }
    }
}).start();

【讨论】:

  • 我现在收到此错误:(已添加到我的帖子中。)
  • 但是您提供的代码确实有效并回答了我的问题。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多