gpt4 book ai didi

java - ListView : getHeight() on a null object reference

转载 作者:行者123 更新时间:2023-12-02 03:28:37 28 4
gpt4 key购买 nike

我有一个子类 ListView我有 onDraw()我尝试动态获取高度的方法。但我得到了 NullPointerException .

错误:

Fatal Exception: java.lang.NullPointerException
at in.justickets.android.view.ExpandedListView.onDraw(ExpandedListView.java:25)
at android.view.View.draw(View.java:14493)
at android.widget.AbsListView.draw(AbsListView.java:3869)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
at android.view.View.getDisplayList(View.java:13328)
at android.view.View.getDisplayList(View.java:13432)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3077)
at android.view.View.getDisplayList(View.java:13328)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.widget.AbsListView.dispatchDraw(AbsListView.java:2467)
at android.view.View.draw(View.java:14496)
at android.widget.AbsListView.draw(AbsListView.java:3869)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13385)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14496)
at android.support.v4.view.ViewPager.draw(ViewPager.java:2336)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2947)
at android.view.View.draw(View.java:14496)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13385)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14496)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13385)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13385)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.getDisplayList(View.java:13385)
at android.view.View.getDisplayList(View.java:13432)
at android.view.View.draw(View.java:14210)
at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
at android.view.View.draw(View.java:14496)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2326)
at android.view.View.getDisplayList(View.java:13390)
at android.view.View.getDisplayList(View.java:13432)
at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1577)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1451)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2420)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2292)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1922)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1039)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5648)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5111)
at java.lang.reflect.Method.invokeNative(Method.java)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(NativeStart.java)

代码:

public class SubListView extends ListView {

private int count = 0;

public SubListView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onDraw(Canvas canvas) {
if (getCount() != count) {
count = getCount();
android.view.ViewGroup.LayoutParams params = getLayoutParams();
if(params!=null) {
params.height = getCount() * (count > 0 ? getChildAt(0).getHeight() : 0);
setLayoutParams(params);

}
}

super.onDraw(canvas);
}

}

最佳答案

问题是调用 getHeight() 时,您的 View 尚未绘制。

您应该在此之前调用 super.onDraw():

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if (getCount() != count) {
count = getCount();
android.view.ViewGroup.LayoutParams params = getLayoutParams();
if(params!=null) {
params.height = getCount() * (count > 0 ? getChildAt(0).getHeight() : 0);
setLayoutParams(params);
}
}
}

关于java - ListView : getHeight() on a null object reference,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38412956/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com