java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.view.View.getBottom()" [英] java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getBottom()' on a null object reference

查看:32
本文介绍了java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.view.View.getBottom()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我构建和运行它时,它运行良好.但是当我远程重新启动应用程序时出现此错误.

It's working fine when I build and run it. But I'm getting this error when I relaunch the app remotely.

这是我唯一使用 views 的地方.它在 ListAdapter 中.

this is the only place where I used views. It's in ListAdapter.

我在 Google 和 StackOverflow 上环顾四周,找到了一些信息.但是,我还没有让它工作.现在我希望我的代码能得到帮助.

I looked around on both Google and StackOverflow and I have found some information. However, I have not got it to work. Now I hope that I can get help with my code.

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getBottom()' on a null object reference
        at android.widget.ListView.fillDown(ListView.java:829)
        at android.widget.ListView.fillFromTop(ListView.java:888)
        at android.widget.ListView.layoutChildren(ListView.java:1958)
        at android.widget.AbsListView.onLayout(AbsListView.java:2991)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
        at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
        at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
        at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:888)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:1075)
        at android.view.View.layout(View.java:22406)
        at android.view.ViewGroup.layout(ViewGroup.java:6594)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:3394)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2863)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1911)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8526)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
        at android.view.Choreographer.doCallbacks(Choreographer.java:761)
        at android.view.Choreographer.doFrame(Choreographer.java:696)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7078)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:974)

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if(getItem(position)!= null) {
            title = getItem(position).getText();
            date = getItem(position).getDate();
            hour = getItem(position).getHour();
            minute = getItem(position).getMinute();
            SimpleDateFormat formatter = new SimpleDateFormat("dd MMMM yyyy");
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(resource, parent, false);
            TextView titleView = (TextView) convertView.findViewById(R.id.title);
            TextView descpView = (TextView) convertView.findViewById(R.id.description);
            TextView dateView = (TextView) convertView.findViewById(R.id.date);
            titleView.setText(title);
            descpView.setText(hour + ":" + minute);
            dateView.setText(formatter.format(date));
        }

        return convertView;
    }

推荐答案

java.lang.NullPointerException:尝试在空对象引用上调用虚方法int android.view.View.getBottom()"

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getBottom()' on a null object reference

在该视图上调用 getBottom() 时,某些视图变为 null,导致 NullPointerException.

Some view is getting null causing NullPointerException when calling getBottom() on that view.

因此,由于应用程序在本地运行而没有此问题;但不是远程,所以答案将是一种猜测.

So, as the app runs locally without this issue; but not remotely, so the answer will be a kind of a guess.

假设 1: getView 返回空值.

原因: convertView被标记为@Nullable,所以从一开始就可以是null,并且如果条件 if(getItem(position)!= null) 不满足,则可以保持为 null;因此 getView() 可以返回一个 null View.

Reason: convertView is marked as @Nullable, so it can be null from the beginning, and can remain null if the condition if(getItem(position)!= null) is not met; and therefore getView() can return a null View.

建议:您需要通过在 if 之外设置 convertView 值来确保 getView 不能返回空值 条件类似于下面的代码片段.

Recommendation: you need to make sure that getView can't return null value by setting the convertView value outside the if condition like the below snippet.

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    if (converView == null) {
        convertView = inflater.inflate(resource, parent, false);
    }

假设 2: convertView 从此表达式为 null convertView = inflater.inflate(resource, parent, false);

Assumption 2: convertView is null from this expression convertView = inflater.inflate(resource, parent, false);

原因:您没有提供传递给 inflate()resource 参数的来源;也许它是一个可以在多个值中变化的变量值;其中之一可以为空;因此也使 convertView 为 null.

Reason: You didn't provide from where you got the resource argument you pass to inflate(); maybe it's a variable value that can change in multiple values; one of which can be null; and therefore making convertView null as well.

建议:

如果 resource 不可更改,您可以通过将其值设置为 R.layout.list_row 来对其进行硬编码;其中 list_row 是列表项的 xml 布局.

If the resource is not changeable, you can hardcode it by setting its value to say R.layout.list_row; where list_row is the xml layout of your list item.

如果它是可变的,那么调试它的值以确保它无论如何都不能为空.

And If it's changeable, then debug its value to make sure it can't be null by any means.

这篇关于java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int android.view.View.getBottom()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆