Why the view provided by onViewCreated() works as non null for one method and as null for the other method?

huangapple go评论77阅读模式
英文:

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 for one method and as null for the other method?

英文:

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();

        ...........................................
    }

'''

Why the view provided by onViewCreated() works as non null for one method and as null for the other method?

答案1

得分: 1

view 不是 null。问题出在 actionsForUISizesOptimizationProcess() 这段代码:

fragmentContainer = view.findViewById(R.id.fragmentContainer);

fragmentContainernullfindViewById() 调用返回了 null

所以,也许在 View 层次结构中没有带有 ID fragmentContainerView

英文:

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.

huangapple
  • 本文由 发表于 2020年8月10日 17:48:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63337845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定