getViewTreeObserver()。Addongloballayoutlistener上的Android null对象引用
我需要在进行操作之前检查布局是否已完全夸大,为此,我使用的代码具有信号量等待,直到两个细节按钮被夸大。该代码大部分时间都可以正常工作,但有时我会遇到致命异常错误: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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论