启动片段的背景线程时,应用会随机崩溃

发布于 2025-02-03 08:32:08 字数 888 浏览 3 评论 0原文

我有此片段的代码:

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    txtAngle = view.findViewById(R.id.textView_angle);

    updateTextThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (threadRunning) {
                txtAngle.setText("1");
            }
        }
    });

    threadRunning = true;
    updateTextThread.start();
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    binding = null;
    threadRunning = false;
}

该应用在第一次导航到此片段时工作正常,但是如果我返回主页并再次导航到此片段,则该应用程序有30%的机会崩溃,投掷> java.lang.nullpoInterException:尝试调用setText line的null对象参考 null对象参考线的虚拟方法'int android.text.layout.getWidth()'。我尝试使用thread.interrupt()停止线程,但它不起作用。

那么是什么原因导致坠机事故,我该如何解决呢?

I have this code for my fragment:

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    txtAngle = view.findViewById(R.id.textView_angle);

    updateTextThread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (threadRunning) {
                txtAngle.setText("1");
            }
        }
    });

    threadRunning = true;
    updateTextThread.start();
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    binding = null;
    threadRunning = false;
}

The app works fine the first time I navigate to this fragment, but if I return to the main page and navigate to this fragment again, the app has a 30% chance to crash, throwing java.lang.NullPointerException: Attempt to invoke virtual method 'int android.text.Layout.getWidth()' on a null object reference for the setText line of the thread. I tried to use Thread.interrupt() to stop the thread but it didn't work.

So what caused the crash and how can I solve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人的夜不怕黑 2025-02-10 08:32:08

您应该在此处注意两件事:

  1. 的两个线程之间共享一个变量
  2. 在渲染线程 /主线程中更新UI之间

使用线程安全变量,例如atomicicboolean或volatile boolean for thread> threadrunning < / code> var ...和双检查锁定或a 锁定为了验证var的值,

您无法保证更新线程循环在更改setText方法之前不会在更改螺纹式var值时……

也最好调用super。 OnDestroyView()在OnDestroyView方法的末尾。

您可以做什么:

使用以下可能性之一(而不是详尽)访问TextView更新到主线程,

You should take care ot two things here :

  1. sharing a variable between two threads
  2. updating UI out of the render thread / main thread

What you should do :

use thread safe variables like AtomicBoolean or a volatile boolean for the threadRunning var... and a double checked locking or a Lock for verifying the value of the var

without this you have no guarantee that you update thread loop is not before the setText method when changing the threadRunning var value...

also, you'd better call super.onDestroyView() at the end of the onDestroyView method.

What you could do :

Dispatch the TextView update from the update thread to the main thread using one of the following possibility (not exhaustive)

  • use a Handler associated with the main Looper
  • use a coroutine or rxJava to dispatch the work to the right thread
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文