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

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

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

问题

这在构建和运行时运行良好。但是在远程重新启动应用程序时出现了这个错误。

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

我在Google和StackOverflow上查找了一些信息。然而,我还没有让它正常工作。现在我希望能够得到有关我的代码的帮助。

java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法'int android.view.View.getBottom()'
        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)
        // 更多堆栈跟踪...
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;
    }
英文:

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

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

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;
    }

答案1

得分: 2

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

在调用 getBottom() 方法时,某个视图获取了空值,导致了 NullPointerException

因此,应用在本地运行时没有此问题;但在远程运行时出现了这个问题,因此答案可能是一种猜测。

假设 1: getView 返回了一个空值。

原因: convertView 被标记为 @Nullable,因此它在一开始可以是 null,如果条件 if(getItem(position)!= null) 不满足,它仍然可以保持为空;因此 getView() 可以返回一个 null 视图

建议: 您需要确保 getView 不能返回空值,可以通过在 if 条件之外设置 convertView 的值,就像下面的代码片段一样。

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

假设 2: 从这个表达式 convertView = inflater.inflate(resource, parent, false); 可以得出 convertView 为空。

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

建议:

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

如果它是可更改的,则通过调试其值,确保它不会以任何方式变为 null。

英文:

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

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.

Assumption 1: getView returns a null value.

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.

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

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

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.

Recommendation:

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.

huangapple
  • 本文由 发表于 2020年5月29日 22:52:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/62088733.html
匿名

发表评论

匿名网友

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

确定