2012-04-08 50 views
1

塊引用安卓ExpandableListView - NullPointerException異常

我在android系統

此使用ExpandableListView是getChildView在適配器類代碼:

@Override 
    public View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if (convertView == null) { 

      LayoutInflater inflater = (LayoutInflater) myContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.childrow, null); 
      holder = new ViewHolder(); 

      holder.tv = (TextView) convertView 
        .findViewById(R.id.textView1); 
          // I added this line of code 
       holder.stop = (Button) convertView.findViewById(R.id.button3); 

      holder.position = groupPosition; 
      convertView.setTag(holder); 
      holder.stop.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

       ViewHolder holder = new ViewHolder(); 
       holder = (ViewHolder) v.getTag(); 

        int position = 0; 

         position = holder.position; ************* here 

        System.out.println("coolapse tag " + position); 

        expList.collapseGroup(position); 


       } 
      }); 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     return convertView; 
    } 

它給NullPointerException異常的線上標有** 有什麼問題?

回答

1

什麼是holder.stopholder.stop不是您設置標籤的視圖,所以當您嘗試執行v.getTag()時,您會在holder中得到空值(順便說一下,我沒有看到holder.stop正在初始化)。不知道你要去的確切內容,但一個辦法解決它是設置在支架上stop視圖,以及:

convertView.setTag(holder); 
holder.stop.setTag(holder); 
holder.stop.setOnClickListener(.... 
+0

這是一類 \t靜態類ViewHolder { \t \t \t \t按鈕停止; \t \t TextView tv; \t \t int position; \t} – Developer 2012-04-08 18:16:45

+0

您不能使用靜態類,每個視圖需要一個持有者實例。 – dmon 2012-04-08 18:22:55

+0

所以我應該用什麼? – Developer 2012-04-08 18:25:59

相關問題