2014-05-12 244 views
0

我有一個列表視圖有兩個textviews和一個按鈕。如果按鈕被點擊,則它變成「停止」按鈕圖像,並且如果另一行按鈕被點擊,則前一個按鈕返回到「播放」按鈕圖像,並且新行按鈕變成「停止」按鈕圖像。所有這些都爲第2-3行工作。滾動後,它停止工作,並給我一個NullPointerException。我猜它與listview的視圖組回收有關。如果任何人都可以幫助我,這將會很棒。Android的Listview按鈕點擊

這裏是代碼爲我的適配器的onclicklistener:

@Override 
    public View getView(final int pos, View ConvertView, ViewGroup parent) { 

     final MyViewHolder holder; 
     if (ConvertView == null){ 
      ConvertView = layoutinflater.inflate(R.layout.row, parent, false); 
      holder = new MyViewHolder();   
      holder.plays = (ImageButton) ConvertView.findViewById(R.id.plays); 
      holder.plays.setFocusable(false); 
      holder.plays.setOnClickListener(this); 
      parents = parent; 
      }else{ 
       holder = (MyViewHolder)ConvertView.getTag(); 
      } 


      play = playdata.get(pos); 
      holder.posturl= play.getposturl(); 






      holder.plays.setTag(pos);  
      ConvertView.setTag(holder); 



      if(mHighlightedPositions[pos]) { 
       holder.plays.setImageResource(R.drawable.ic_action_stop); 
      }else { 
       holder.plays.setImageResource(R.drawable.ic_action_play); 
      } 





     play = playdata.get(pos); 
     holder.play = play; 

     return ConvertView; 



    } 

    @Override 
    public void onClick(View view) { 

     int position = (Integer)view.getTag(); 
      Log.d("clicked", "Button row pos click: " + position); 


      // Toggle background resource 
      LinearLayout layout = (LinearLayout)view.getParent(); 
      ImageButton button = (ImageButton)layout.getChildAt(3); 

      if(initialposition!=-1) 
      {if(mHighlightedPositions[position]) { 
       button.setImageResource(R.drawable.ic_action_play); 
       mHighlightedPositions[position] = false; 
      }else { 
       button.setImageResource(R.drawable.ic_action_stop); 
       mHighlightedPositions[position] = true; 
       mHighlightedPositions[initialposition]=false; 
       LinearLayout parentlayout = (LinearLayout) parents.getChildAt(initialposition); 
       ImageButton btn = (ImageButton) parentlayout.getChildAt(3); 
       btn.setImageResource(R.drawable.ic_action_play); 

      } 
      }else { 
       button.setImageResource(R.drawable.ic_action_stop); 
       mHighlightedPositions[position] = true; 
      } 
      initialposition = position; 
    } 

任何幫助表示讚賞!謝謝!

編輯:我得到的NullPointerException在此位:

LinearLayout parentlayout = (LinearLayout) parents.getChildAt(initialposition); 
        ImageButton btn = (ImageButton) parentlayout.getChildAt(3); 
        btn.setImageResource(R.drawable.ic_action_play); 

而且,記錄的NullPointerException異常,並滾動該parentlayout變成空時,它的。

+0

是什麼mHihglightedPositions和它在哪兒設置? – zgc7009

回答

0

如果沒有佈局的第三個孩子或者沒有佈局,您應該捕獲錯誤。嘗試檢查,layoutbutton是否真的有內容你想要的:

LinearLayout layout = (LinearLayout)view.getParent(); 
if(layout != null) { 
    ImageButton button = (ImageButton)layout.getChildAt(3); 
} else { 
    Log.d("Error", "No Parent Layout"); 
} 
if(button != null) { 
    //do your stuff 
} else { 
    Log.d("Error", "No Third Child"); 
}