2016-07-16 18 views
0

サブクラスがListViewで、onDraw()のメソッドがあり、高さを動的に取得しようとしています。しかし、私はNullPointerExceptionを取得しています。ListView:ヌルオブジェクト参照のgetHeight()

エラー

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

を取得するには通常、あなたは確認する必要があります。 @Unknownのコメントを参照してください。 – xdevs23

答えて

2

問題がある、あなたのViewgetHeight()を呼び出すときはまだ描かれていません。

あなたはその前にsuper.onDraw()を呼び出す必要があります:あなたは `NullPointerException`を取得するときにオブジェクトが初期化されていない理由

@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); 
     } 
    } 
} 
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; } 
関連する問題