为什么 onViewCreated() 提供的视图对于一种方法作为非空值而对于另一种方法作为空值?
Why the view provided by onViewCreated() works as non null for one method and as null for the other method?
为什么 onViewCreated() 提供的视图在简单的 findViews(View view) 中作为非 null 而对于 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); //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();
...........................................
}
'''
view
不是 null
。问题是 actionsForUISizesOptimizationProcess()
:
中的这段代码
fragmentContainer = view.findViewById(R.id.fragmentContainer);
fragmentContainer
为空。对 findViewById()
的调用正在返回 null
那么,View
层次结构中可能没有 ID 为 fragmentContainer
的 View
?
在您的 xml 文件中,检查您的 FrameLayout 的 ID 是否为 fragmentContainer
并且存在 no typo
您的 XML 文件中可能存在拼写错误,其中 id 应该是 fragmentContainer,这就是 findViewById() 找不到它的原因。
为什么 onViewCreated() 提供的视图在简单的 findViews(View view) 中作为非 null 而对于 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); //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();
...........................................
}
'''
view
不是 null
。问题是 actionsForUISizesOptimizationProcess()
:
fragmentContainer = view.findViewById(R.id.fragmentContainer);
fragmentContainer
为空。对 findViewById()
的调用正在返回 null
那么,View
层次结构中可能没有 ID 为 fragmentContainer
的 View
?
在您的 xml 文件中,检查您的 FrameLayout 的 ID 是否为 fragmentContainer
并且存在 no typo
您的 XML 文件中可能存在拼写错误,其中 id 应该是 fragmentContainer,这就是 findViewById() 找不到它的原因。