我有一个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);
}
}
答案 0 :(得分:2)
问题是,在调用View
时,您的getHeight()
尚未绘制。
您应该在此之前致电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);
}
}
}
答案 1 :(得分:0)
用它来获得高度
private int getHeightofListView() {
ListAdapter mAdapter = lv.getAdapter();
int lvElementsheight = 0;
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, lv);
mView.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
lvElementsheight += mView.getMeasuredHeight();
}
return lvElementsheight; }