getViewTreeObserver()。Addongloballayoutlistener上的Android null对象引用

发布于 2025-01-30 15:17:57 字数 2270 浏览 1 评论 0原文

我需要在进行操作之前检查布局是否已完全夸大,为此,我使用的代码具有信号量等待,直到两个细节按钮被夸大。该代码大部分时间都可以正常工作,但有时我会遇到致命异常错误:java.lang.nullpointerexception:尝试调用虚拟方法'boolean java.util.arraylist.add(java.lang.object)'null对象参考。有人知道为什么以及如何修复它吗?这是代码和完整日志:

    final Semaphore semaphore = new Semaphore(0);
    LayoutKBDReferencesHelper.instance().
            getLayoutKBDReferences().getBtn0().
            getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        semaphore.release();
                    }
                });
    try {
        semaphore.acquire();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } 

final Semaphore semaphore1 = new Semaphore(0);
    LayoutKBDReferencesHelper.instance().getLayoutKBDReferences().getBtn1().
            getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        semaphore1.release();
                    }
                });
    try {
        semaphore1.acquire();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

日志:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at android.view.ViewTreeObserver$CopyOnWriteArray.add(ViewTreeObserver.java:1181)
at android.view.ViewTreeObserver.addOnGlobalLayoutListener(ViewTreeObserver.java:550)
at com.example.layout.SampleLayout.showKBD(SampleLayout.java:1025)
at com.example.layout.SampleLayout.OutputCallbacks.Text(OutputCallbacks.java:108)
at com.example.layout.SampleLayout.Base.Text(SourceFile:5)
at com.example.layout.SampleLayout.LayoutTest(SourceFile)
at com.example.layout.SampleLayout.LayoutTest(SourceFile:4)
at com.example.layout.SampleLayout.LayoutTest(SourceFile:1)
at com.example.layout.SampleLayout.SampleLayout$4.run(SampleLayout.java:378)
at java.lang.Thread.run(Thread.java:764)

i need to check if a layout has been completely inflated before doing an operation, for that, I'm using the code below that has a semaphore that waits until a two specifics buttons is inflated. The code works fine most of the time but sometimes I get the Fatal Exception error: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference. Does anyone know why and how to fix it? Here is code and the complete log:

    final Semaphore semaphore = new Semaphore(0);
    LayoutKBDReferencesHelper.instance().
            getLayoutKBDReferences().getBtn0().
            getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        semaphore.release();
                    }
                });
    try {
        semaphore.acquire();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } 

final Semaphore semaphore1 = new Semaphore(0);
    LayoutKBDReferencesHelper.instance().getLayoutKBDReferences().getBtn1().
            getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        semaphore1.release();
                    }
                });
    try {
        semaphore1.acquire();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

Log:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at android.view.ViewTreeObserver$CopyOnWriteArray.add(ViewTreeObserver.java:1181)
at android.view.ViewTreeObserver.addOnGlobalLayoutListener(ViewTreeObserver.java:550)
at com.example.layout.SampleLayout.showKBD(SampleLayout.java:1025)
at com.example.layout.SampleLayout.OutputCallbacks.Text(OutputCallbacks.java:108)
at com.example.layout.SampleLayout.Base.Text(SourceFile:5)
at com.example.layout.SampleLayout.LayoutTest(SourceFile)
at com.example.layout.SampleLayout.LayoutTest(SourceFile:4)
at com.example.layout.SampleLayout.LayoutTest(SourceFile:1)
at com.example.layout.SampleLayout.SampleLayout$4.run(SampleLayout.java:378)
at java.lang.Thread.run(Thread.java:764)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文