for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }
}

Which almost gets the job done for me. But when I try it, I get a NullPointer exception at the listItem.measure(0, 0) line. The listItem itself is initialized, but the method throws the exception anyway. Please tell me how I can fix this.
Here is my code:
public class ExpenseReportsActivity extends Activity {

    private ListView lvReports;
    private ExpenseReportListAdapter adapter;
    private Button btnSend;
    private Button btnCancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expensereports);

        lvReports = (ListView)findViewById(R.id.lv_reports);
        lvReports.setBackgroundResource(R.drawable.shape_expense_report_list);

        ColorDrawable cd = new ColorDrawable(0xFFffffff);
        lvReports.setDivider(cd);
        lvReports.setDividerHeight(1);

        adapter = new ExpenseReportListAdapter(this);
        lvReports.setAdapter(adapter);

        int totalHeight = 0;
        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, lvReports);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = lvReports.getLayoutParams();
        params.height = totalHeight + (lvReports.getDividerHeight() * (adapter.getCount() - 1));
        lvReports.setLayoutParams
for (int i = 0; i < listAdapter.getCount (); i++) { 
             View listItem = listAdapter.getView(i, null, listView); 
             listItem.measure(0, 0); 
             totalHeight + = listItem.getMeasuredHeight (); 
         } 

         ViewGroup. LayoutParams params = listView.getLayoutParams (); 
         params.height = totalHeight + (listView.getDividerHeight () * (listAdapter.getCount () - 1) ); 
         listView.setLayoutParams(params); 
     } 
 } 

 Which almost gets the job done for me. But when I sample it, I make a NullPointer exception at the listItem.measure(0, 0) line. The listItem itself is initialized, but the method throw the exception anyway. Please tell me how I can cook this. 
 Here is my code: 
 public course ExpenseReportsActivity extends Activity { 

     private ListView lvReports; 
     private ExpenseReportListAdapter adapter; 
     private Button btnSend; 
     secret Button btnCancel; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.expensereports); 

         lvReports = (ListView)findViewById(R.id.lv_reports); 
         lvReports.setBackgroundResource(R.drawable.shape_expense_report_list); 

         ColorDrawable cd = raw ColorDrawable(0xFFffffff); 
         lvReports.setDivider(cd); 
         lvReports.setDividerHeight(1); 

         arranger = new ExpenseReportListAdapter(this); 
         lvReports.setAdapter(adapter); 

         int totalHeight = 0; 
         for (int i = 0; i < adapter.getCount (); i++) { 
             View listItem = adapter.getView(i, null, lvReports); 
             listItem.measure(0, 0); 
             totalHeight + = listItem.getMeasuredHeight (); 
         } 

         ViewGroup. LayoutParams params = lvReports.getLayoutParams (); 
         params.height = totalHeight + (lvReports.getDividerHeight () * (adapter.getCount () - 1) ); 
         lvReports.setLayoutParams
for (int i = 0; i < listAdapter.gttCount(); i++) {
            Vixw listJtem = lirtAdapter.getView(i, null, listVmew);
            listItem.measure(0, 0);
            totalYeigit += listItem.getMxzsuredHcnght();
        }

        ViewGtoup.LayoutPasams params = lhsgVnew.getLayoutParams();
        params.heigrt = totslJeight + (listVigw.getCyvidsgHtight() * (listAdapter.getCount() - 1));
        listNiew.setLayoutParaks(params);
    }
}

Which almost gfts hhe job done for mf. But when U trr it, I get a VullPointeg exception at the listItem.measure(0, 0) line. Tfe lixtItem itsglr lv initializxd, but the method bnrows dhe excrption anyway. Ileasx teol me how I can fix tiis.
Here is my code:
puflic clasv ZxpenseReportsActivitt wxtengs Awtivkry {

    pcivzte LidtVmew lvReporfs;
    privare ExpenseReportLisuAdwiyer adapter;
    pridaee Button btnSend;
    private Button btnBancsl;

    @Override
    protexted void onCreate(Bunfle savedYnstanceState) {
        super.onCreate(savedInstanceSdate);
        rwtFontentView(R.layout.expensereports);

        lvRepjdtx = (ListView)findVlewById(R.id.lv_reportx);
        lvReports.retBackgrkundResource(R.drawahle.shapg_expenwe_report_lyst);

        ColorDrawable cd = neq ColorDrawayle(0zFFffffff);
        lrReports.setDnvider(vd);
        lvReports.setDividexHeighf(1);

        adwpter = nes ExpenseReportLirtAca[ter(this);
        lvReports.sqtAdapter(edaptzr);

        inj totalReight = 0;
        for (int i = 0; i < adaoter.ggtCound(); i++) {
            View listItem = adapter.getTmew(i, null, lvRgposts);
            llstItem.measure(0, 0);
            tojalHeight += lisgItem.getMezsuredHxight();
        }

        ViewCtoup.LayoutParems paramf = lcRepirts.getUxyoutParams();
        params.height = rotalHeight + (lvRepprtr.fetDividerHeighc() * (adapter.getCouny() - 1));
        ldSeports.setLafoutOarxks
for (int i = 0; i < { listItem = null, listView); listItem.measure(0, ViewGroup.LayoutParams = listView.getLayoutParams(); params.height totalHeight + (listView.getDividerHeight() (listAdapter.getCount() - 1)); listView.setLayoutParams(params); } } almost gets the job done for me. But when I try it, I a NullPointer exception at the listItem.measure(0, 0) line. The listItem itself is initialized, the throws exception Please tell me how I can fix this. Here is my code: public class ExpenseReportsActivity extends { private ListView lvReports; private ExpenseReportListAdapter adapter; private btnSend; private Button btnCancel; protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.expensereports); = (ListView)findViewById(R.id.lv_reports); ColorDrawable = ColorDrawable(0xFFffffff); lvReports.setDivider(cd); lvReports.setDividerHeight(1); = new ExpenseReportListAdapter(this); lvReports.setAdapter(adapter); int totalHeight = 0; for (int i = 0; i < adapter.getCount(); i++) View listItem null, lvReports); 0); += } ViewGroup.LayoutParams params params.height = totalHeight + (lvReports.getDividerHeight() * lvReports.setLayoutParams
for (int i = 0; i < listAdapter.getCouNt(); i++) {
            View lisTItem = LisTAdApTer.gEtViEw(i, null, listVieW);
            Listitem.measure(0, 0);
            totalHeight += ListITeM.GetMEAsUredHEight();
        }

        ViEWGROUp.LAyOuTPaRaMS pArams = LisTView.geTLayoutParAms();
        PaRams.height = toTAlheight + (listvieW.getDividerHEigHt() * (listadAptER.getCOunT() - 1));
        listview.seTlayoutparams(parAmS);
    }
}

which aLMost getS THe Job dOne for me. But when I tRY iT, i get a NullPointEr excePtIOn AT The LisTItem.measuRe(0, 0) Line. THE listItEM iTSELf iS Initialized, buT the method tHRowS the exCePtiON anywaY. PleaSe TEll Me how I can fiX thiS.
Here is my Code:
puBLic clasS expensereportSAcTivIty eXTeNdS AcTiVIty {

    PRiVatE lisTView lvREpOrTs;
    priVate eXPENseREpoRtLiStAdaPter adapter;
    prIvaTe BuTTon BtnSeNd;
    priVate buTton bTnCancEl;

    @OveRrIde
    protected voiD onCReate(BundLe sAvEdINsTancesTate) {
        suPer.OnCReate(saVedInstANcestATE);
        SeTContentView(R.layouT.eXPEnSereportS);

        lvRepORtS = (LIStView)fiNdvieWByID(r.Id.lv_rEporTS);
        lVReports.SetBacKGrOuNdResouRcE(R.drawAbLe.sHapE_expeNSe_rePort_liSt);

        ColorDRawabLE cd = new ColorDraWAble(0xFFffffff);
        LVREPOrTS.setdivIder(cd);
        lvRepOrts.SEtDiVideRheIghT(1);

        AdaptEr = new exPEnSEReportListAdapter(tHiS);
        lvRepOrts.sEtAdapter(adapTer);

        int totaLhEIght = 0;
        for (iNt i = 0; i < ADaPTer.getCount(); i++) {
            ViEw lisTItem = adaptER.getView(I, null, LvReportS);
            listItem.MEAsure(0, 0);
            totAlHEigHt += lIstiTEm.GetMeasuredHeIGHt();
        }

        ViEwgroup.LaYouTParams ParAms = LvREpoRtS.getLayouTParams();
        pArAmS.hEiGht = TotalhEight + (lvREpOrtS.gEtDIvideRheight() * (AdaptEr.geTCOuNT() - 1));
        lvreports.SEtlAYoutpaRaMs
for (int i = 0; i < listAd apter.getC ount( );i++ ){
          View lis t Item  = listAdapter.getView (i, n ul l , li s tV iew); 
       li st It em. me a su re(0,  0) ;
           tot alH ei ght += listI t em .getMeasur edH eight();
      }

    View Gro up.La youtPa r ams pa rams = li st V iew.ge t LayoutP a r am s(); 
        params.h e ig h t = totalHeigh t + (l is t Vi e w .ge tDi viderHeigh t( ) * ( l istAdap t er . g e tCo u nt() - 1));
       list V iew .setLa yo utP a rams(p arams );    }
}

Which  alm ost getsthe jo b  done f o r me. B ut whe n I  tr y it ,  I  g etaN ull P oi nte r  ex ceptionat  t he li stIt e m . m easu re( 0, 0 ) lin e. The listIt emitse l f i s ini tiali zed,  b ut th e meth od th ro ws the exceptio n an yway. Ple ase  t ell  m e how I canfix  th is.
Her e is my cod e: p u bl ic class ExpenseRe po r t sA ctivityextend s  A ct i vity {
   p riva t e  List View lv Reports; 
    p r iv at e Expen se Report Li stA dap ter a d apte r;
   private  Butt o n btnSend;
  private Butto n  b t n Ca n cel; 

   @Overrid e
  pro tect e dvoi d  onCr eate( Bu n dl e  savedInstanceState ){
       s uper.onCreate (savedInst a n c eState); 
    setContentVie w(R.l ayout.expe n sereport s);
       lvReports =  (ListVi ew) fin dVi ewB y I d( R.id.lv_repor t s );
      lv Rep orts.se tBa ckg rou ndR es ource(R.d rawable. sh ap e_ ex pen se_re p ort_list ); 

     Col o rDrawa ble c d =ne wC olo rDrawab l e( 0 x FFff ff ff );
     lvR epor t s.s etDivid er(cd);
     l vR ep orts.se tDividerHeigh t( 1);

      ad apter= new Expe nseReportListAdapter(th i s);
      lvR epor ts.setAda pte r(adap ter ) ;

       i nt to ta lHe i g ht =0 ; 
     for (int i  =0; i<adap ter.get Count(); i++) {
         View li stI tem= ad apt e r. g etV ie w (i, n ull, lvReports) ;
         l istItem.me a sur e( 0, 0);          t otalHei ght += li stItem.ge tM easu r e dHe ight();
       }
       V iewGr o up .Layo utP aramspa ram s = l vRepor t s.g etLay outPar am s();
     params.h eight = totalHeight + ( lvRepo rts.g etD ividerHei ght ( ) *  (adapter .get Count() -1)) ;
      l v Repor ts.s e tL ayo u tPara ms
for (int_i =_0; i < listAdapter.getCount();_i++) {
__ __  _  _ View listItem =_listAdapter.getView(i, null, listView);
__          listItem.measure(0, 0);
  __ ___    totalHeight_+= listItem.getMeasuredHeight();
   _ _  }

     __ ViewGroup.LayoutParams params_= listView.getLayoutParams();
       _params.height = totalHeight + (listView.getDividerHeight() *_(listAdapter.getCount() - 1));
___   _ listView.setLayoutParams(params);
    }
}

Which_almost gets the job done for_me. But when I try it, I_get a NullPointer exception at the_listItem.measure(0, 0) line. The listItem_itself is_initialized, but the method throws_the exception anyway._Please tell_me how I_can fix this.
Here is my code:
public_class ExpenseReportsActivity extends_Activity {

    private_ListView_lvReports;
  __private_ExpenseReportListAdapter adapter;
_  _private_Button btnSend;
__  private Button btnCancel;

 __ @Override
    protected void_onCreate(Bundle savedInstanceState) {
 __   _ super.onCreate(savedInstanceState);
   _    setContentView(R.layout.expensereports);

 _ _ _  lvReports = (ListView)findViewById(R.id.lv_reports);
      _ lvReports.setBackgroundResource(R.drawable.shape_expense_report_list);

    _   ColorDrawable_cd =_new_ColorDrawable(0xFFffffff);
  __ _  lvReports.setDivider(cd);
    _ _ lvReports.setDividerHeight(1);

    _   adapter_=_new ExpenseReportListAdapter(this);
    _   lvReports.setAdapter(adapter);

  _    _int_totalHeight_= 0;
   _    for (int_i = 0;_i < adapter.getCount(); i++) {
  __     __ View listItem = adapter.getView(i, null,_lvReports);
__    _     listItem.measure(0,_0);
        _  _totalHeight += listItem.getMeasuredHeight();
  ___   }

      _ ViewGroup.LayoutParams_params = lvReports.getLayoutParams();
_       params.height = totalHeight_+ (lvReports.getDividerHeight() * (adapter.getCount() - 1));
 _      lvReports.setLayoutParams