ratingBar.getTag()返回null

时间:2014-07-01 14:58:48

标签: android android-listview ratingbar

我正在eclipse中制作一个Android应用程序。我的应用程序有一个列表,其中包含ListView中的RatingBar。我正在使用自定义ArrayAdapter来显示列表。下面是我的代码....它会在语句ratingBar.getTag()中抛出nullpointerexception;我无法弄清楚为什么?

public class MyChoicesAdapter extends ArrayAdapter<MyChoice> {

private LayoutInflater mInflater;

private ArrayList<MyChoice> myChoicesList;

private static final String TAG = "MyChoiceAdapter";
private Context c;

public MyChoicesAdapter(Context ctx, int viewResourceId,
        ArrayList<MyChoice> myChoicesList) {
    super(ctx, viewResourceId, myChoicesList);
    c=ctx;
    this.myChoicesList = new ArrayList<MyChoice>();
    this.myChoicesList.addAll(myChoicesList);

}

private class ViewHolder
{
  TextView name;
  CheckBox cb;
  RatingBar rb;
  TextView naText;
  TextView gotItText;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;

    Log.v(TAG, String.valueOf(position));

    if (convertView == null)
    {

        mInflater = (LayoutInflater)c.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);

       convertView = mInflater.inflate(R.layout.my_choices_row, null);

      holder = new ViewHolder();
      holder.name = (TextView)convertView.findViewById(R.id.my_choices_row_text);
      holder.cb = (CheckBox) convertView.findViewById(R.id.my_choices_row_checkBox);
      holder.rb = (RatingBar) convertView.findViewById(R.id.my_choices_row_rating);
      holder.naText = (TextView)convertView.findViewById(R.id.my_choices_row_na_text);
      holder.gotItText = (TextView)convertView.findViewById(R.id.my_choices_row_got_text);

      convertView.setTag(holder);

      holder.cb.setOnClickListener( new View.OnClickListener() 
      {

         public void onClick(View v)  
         {
             CheckBox cb = (CheckBox) v;
             MyChoice _choice = (MyChoice) cb.getTag();
             _choice.setSelected(cb.isChecked());
         }
      });

      holder.rb.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar, float rating,
            boolean fromUser) {

             RatingBar rb = ratingBar;
             if(rb == null)
                Log.v(TAG, "rb is null");
             if(rb.getTag()==null)
                Log.v(TAG, "rb.getTag() is null");
             MyChoice _choice = (MyChoice)rb.getTag();
             _choice.setRating(rating);
             Log.v(TAG, String.valueOf(rating));
            }
        });
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    MyChoice choice = myChoicesList.get(position);
    holder.name.setText(choice.getName());
    holder.cb.setChecked(choice.isSelected());
    if(choice.getNA().equalsIgnoreCase("N/A"))
        holder.naText.setText(choice.getNA());
    if(choice.getGotIt().equalsIgnoreCase("Got it!"))
    holder.gotItText.setText(choice.getGotIt());

    holder.rb.setRating(choice.getRating());
    holder.rb.setTag(choice);
    holder.cb.setTag(choice);


    return convertView;

}

  }

这是logcat的一部分

07-02 06:58:31.330:V / MyChoiceAdapter(989):rb.getTag()为null 07-02 06:58:31.330:D / AndroidRuntime(989):关闭VM 07-02 06:58:31.330:W / dalvikvm(989):threadid = 1:线程退出未捕获异常(组= 0x40a13300) 07-02 06:58:31.440:E / AndroidRuntime(989):致命异常:主要 07-02 06:58:31.440:E / AndroidRuntime(989):java.lang.NullPointerException 07-02 06:58:31.440:E / AndroidRuntime(989):at com.cybergeniesolutions.scentsysquirrel.MyChoicesAdapter $ 2.onRatingChanged(MyChoicesAdapter.java:89) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.RatingBar.dispatchRatingChange(RatingBar.java:318) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.RatingBar.onProgressRefresh(RatingBar.java:261) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.ProgressBar.doRefreshProgress(ProgressBar.java:660) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.ProgressBar.refreshProgress(ProgressBar.java:672) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.ProgressBar.setProgress(ProgressBar.java:719) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.ProgressBar.setProgress(ProgressBar.java:700) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.RatingBar.setRating(RatingBar.java:197) 07-02 06:58:31.440:E / AndroidRuntime(989):at com.cybergeniesolutions.scentsysquirrel.MyChoicesAdapter.getView(MyChoicesAdapter.java:107) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.AbsListView.obtainView(AbsListView.java:2267) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.ListView.makeAndAddView(ListView.java:1769) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.ListView.fillDown(ListView.java:672) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.ListView.fillFromTop(ListView.java:733) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.ListView.layoutChildren(ListView.java:1622) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.AbsListView.onLayout(AbsListView.java:2102) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.View.layout(View.java:13754) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.ViewGroup.layout(ViewGroup.java:4362) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.LinearLayout.onLayout(LinearLayout.java:1420) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.View.layout(View.java:13754) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.ViewGroup.layout(ViewGroup.java:4362) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.View.layout(View.java:13754) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.ViewGroup.layout(ViewGroup.java:4362) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.LinearLayout.onLayout(LinearLayout.java:1420) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.View.layout(View.java:13754) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.ViewGroup.layout(ViewGroup.java:4362) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.widget.FrameLayout.onLayout(FrameLayout.java:448) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.View.layout(View.java:13754) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.ViewGroup.layout(ViewGroup.java:4362) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4212) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:725) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.Choreographer.doCallbacks(Choreographer.java:555) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.Choreographer.doFrame(Choreographer.java:525) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:711) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.os.Handler.handleCallback(Handler.java:615) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.os.Handler.dispatchMessage(Handler.java:92) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.os.Looper.loop(Looper.java:137) 07-02 06:58:31.440:E / AndroidRuntime(989):在android.app.ActivityThread.main(ActivityThread.java:4745) 07-02 06:58:31.440:E / AndroidRuntime(989):at java.lang.reflect.Method.invokeNative(Native Method) 07-02 06:58:31.440:E / AndroidRuntime(989):at java.lang.reflect.Method.invoke(Method.java:511) 07-02 06:58:31.440:E / AndroidRuntime(989):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 07-02 06:58:31.440:E / AndroidRuntime(989):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 07-02 06:58:31.440:E / AndroidRuntime(989):at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案