英文:
Why the view provided by onViewCreated() works as non null for one method and as null for the other method?
问题
为什么在onViewCreated()
方法中提供的视图在简单的findViews(View view)
方法中可以正常工作,但对于另一个用于OnWindowFocusChangeListener(...)
的方法会抛出NullPointerException?
我不知道该如何解决这个问题。我的意思是,我不知道要尝试什么。
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
findViews(view);
.....................................
view.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
@Override
public void onWindowFocusChanged(final boolean hasFocus) {
actionsForUISizesOptimizationProcess(hasFocus, view);
}
});
}
void actionsForUISizesOptimizationProcess(boolean hasFocus, View view){
FrameLayout fragmentContainer = view.findViewById(R.id.fragmentContainer); // 调试显示此行中的视图为空
int displayWidth = fragmentContainer.getWidth(); // java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法'int android.widget.FrameLayout.getWidth()'
int displayHeight = fragmentContainer.getHeight();
...........................................
}
英文:
Why the view provided by onViewCreated() works as non null in simple findViews(View view) but for the other method which is for OnWindowFocusChangeListener(...) throws NullPointerException?
I have no idea what to do to solve this problem. I mean, I have no idea what to try.
'''
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
findViews(view);
.....................................
view.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
@Override
public void onWindowFocusChanged(final boolean hasFocus) {
actionsForUISizesOptimizationProcess(hasFocus,view);
}
});
}
void actionsForUISizesOptimizationProcess(boolean hasFocus, View view){
FrameLayout fragmentContainer = view.findViewById(R.id.fragmentContainer); //debugging showed that the view in this line is null
int displayWidth = fragmentContainer.getWidth(); //java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.FrameLayout.getWidth()' on a null object reference
int displayHeight = fragmentContainer.getHeight();
...........................................
}
'''
答案1
得分: 1
view
不是 null
。问题出在 actionsForUISizesOptimizationProcess()
这段代码:
fragmentContainer = view.findViewById(R.id.fragmentContainer);
fragmentContainer
为 null
。findViewById()
调用返回了 null
。
所以,也许在 View
层次结构中没有带有 ID fragmentContainer
的 View
?
英文:
view
is not null
. The problem is this code in actionsForUISizesOptimizationProcess()
:
fragmentContainer = view.findViewById(R.id.fragmentContainer);
fragmentContainer
is null. The call to findViewById()
is returning null
So, maybe there is no View
with ID fragmentContainer
in the View
hierarchy?
答案2
得分: 1
在你的XML文件中,检查FrameLayout的ID是否为fragmentContainer
,并且是否存在no typo
。
英文:
In your xml file, check if the id of your FrameLayout is fragmentContainer
and there exists no typo
答案3
得分: 0
在你的XML文件中很可能有一个拼写错误,应该是“fragmentContainer”,这就是为令findViewById()找不到它的原因。
英文:
There is likely a a typo in your XML file where the id is supposed to be fragmentContainer which is why the findViewById() is not finding it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论