自定义列表视图中的空指针原因

时间:2012-08-24 05:16:13

标签: java android

我正在开发一个应用程序,其中我实现了CustomListview一个图标,raitingbar,复选框。现在一切都显示正常,但当我插入一个按钮它得到我nullpointer error.i知道nullpointer错误导致在没有初始化控件的情况...但我已经尝试到目前为止,不知道这里的问题是什么。 ...

我已经使用一个viewHold来声明所有Row项..

package com.AppFavorits;

import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

class ViewHolder {

    public ImageView imgvFavrowiconappicon;
    public TextView txvxFavrowiconappname;
    public RatingBar ratingBar1;
    public CheckBox chkbxFavrowsel;
    public Button btnFavrowOpen;




  ViewHolder(View base) {
    this.ratingBar1=(RatingBar)base.findViewById(R.id.ratingBar1);
    this.ratingBar1.setFocusable(false);
    this.txvxFavrowiconappname=(TextView)base.findViewById(R.id.txvxFavrowiconappname);
    this.imgvFavrowiconappicon=(ImageView)base.findViewById(R.id.imgvFavrowiconappicon);
    this.chkbxFavrowsel = (CheckBox)base.findViewById(R.id.chkbxFavrowsel);
    this.btnFavrowOpen= (Button)base.findViewWithTag(R.id.btnFavrowOpen); 
    this.chkbxFavrowsel.setFocusable(false);

  }
}

CustomAdapter

class RatingAdapter extends ArrayAdapter<RowModel> implements OnClickListener {
        private ArrayList<Model> mlist;
        boolean[] checkBoxState;

        RatingAdapter(ArrayList<RowModel> list, ArrayList<Model> mlist) {
            super(Favorites.this, R.layout.rowfavorites,
                    R.id.txvxFavrowiconappname, list);
            checkBoxState = new boolean[list.size()];
            this.mlist = mlist;
        }

        public View getView(final int position, View convertView,
                ViewGroup parent) {
            View row = super.getView(position, convertView, parent);

            holder = (ViewHolder) row.getTag();
            if (convertView == null) {
                holder = new ViewHolder(row);
                row.setTag(holder);


            } else {
                row = convertView;
                ((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
                        .get(position));
            }

            RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() {
                public void onRatingChanged(RatingBar ratingBar, float rating,
                        boolean fromTouch) {
                    Integer myPosition = (Integer) ratingBar.getTag();
                    RowModel model = getModel(myPosition);
                    model.rating = rating;
                    flFavRate[position] = rating;
                    // stored listitem rating in array
                    datasource.open();
                    datasource.UpdateRating(String.valueOf(rating), String.valueOf(position+1));
                    datasource.close();


                }
            };
            holder.ratingBar1.setOnRatingBarChangeListener(l);


             holder.chkbxFavrowsel
                    .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            Model element = (Model) holder.chkbxFavrowsel
                                    .getTag();
                            element.setSelected(buttonView.isChecked());
                            bSelected[position] = isChecked;
                            element.setsizeInc(sizeincrement);
                            // if (holder.chkbxFavrowsel.isChecked() ==
                            // isChecked) {
                            boolean bAlreadyfav = false;
                            String prefixlink = "https://play.google.com/store/apps/details?id=";
                            ShrdDatasource.open();
                            ArrayList<Comment> alDSPackagenm = ShrdDatasource.getAllPackage();
                            ArrayList<Comment> alDSRating = ShrdDatasource.getAllRating();
                            ArrayList<Comment> alDSID = ShrdDatasource.getID();
                            Log.i(TAG, "First Package cmp"+  alDSPackagenm);
                            ShrdDatasource.close();
                            int i = 0 ;
                            if (alDSPackagenm != null) {
                                for (i = 0; i < alDSPackagenm.size(); i++) {
                                    if(alDSPackagenm.get(i).toString().equalsIgnoreCase(prefixlink + alPackagenm.get(position).toString() + "1x2optrue") || alDSPackagenm.get(i).toString().equalsIgnoreCase(prefixlink + alPackagenm.get(position).toString() + "1x2opfalse"))
                                    {
                                        bAlreadyfav = true;
                                        break;
                                    }
                                }

                            }
                            GolbPosition = position;
                            if (bAlreadyfav == true) {
                                Log.i(TAG, "Share positionis "+i);
                                Log.i(TAG, "Current positionis "+position);
                                Log.i(TAG,"ShrdDatasource Rating" + alDSRating.get(i).toString());
                                Log.i(TAG,"Favorite Rating" + String.valueOf(flFavRate[GolbPosition]));


                                if(alDSRating.get(i).toString().equalsIgnoreCase(String.valueOf(flFavRate[GolbPosition])))
                                {
                                    Toast.makeText(getBaseContext(), " App already in Share list ",
                                        Toast.LENGTH_LONG).show();
                                }
                                else
                                {
                                    //alid
                                    /*GolbPosition = position;
                                    new GETFavTask().execute();*/

                                    //UpdateRating
                                    ShrdDatasource.open();
                                    ShrdDatasource.UpdateRating(String.valueOf(flFavRate[GolbPosition]), alDSID.get(i).toString());
                                    ShrdDatasource.close();


                                    //Update Query fire
                                }
                            } else {
                                //Log.i(TAG, "First Package cmp"+ alDSPackagenm.get(position).toString());
                                Log.i(TAG, "Second Package cmp"+  alPackagenm.get(position).toString());
                                GolbPosition = position;
                                new GETFavTask().execute();

                                // add item in Database when user get select
                            }

                        }
                    });
                RowModel model = getModel(position);
                ViewHolder holder = (ViewHolder) row.getTag();
                holder.ratingBar1.setTag(new Integer(position));
                //holder.btnFavrowOpen.setTag(mlist.get(position));
                holder.ratingBar1.setRating(model.rating);
                holder.imgvFavrowiconappicon.setImageDrawable(drblAlIcon[position]);
                holder.txvxFavrowiconappname.setText(alAppName.get(position)
                    .toString());
                holder.chkbxFavrowsel.setChecked(mlist.get(position).isSelected());
                holder.chkbxFavrowsel.setTag(mlist.get(position));

                holder.btnFavrowOpen.setOnClickListener(new View.OnClickListener() {
                     public void onClick(View v) {
                        // OpenApp(alPackagenm.get(position).toString());
                     }
                 });
                try
                {
                if (alAppRating.get(position) == null | alAppRating.get(position).toString().equalsIgnoreCase("null")) {
                        holder.ratingBar1.setRating(0.0f);
                } else {
                    holder.ratingBar1.setRating(Float.parseFloat(alAppRating
                            .get(position).toString()));
                }
                }catch (Exception e) {
                    e.printStackTrace();
                }
                //holder.ratingBar1.setRating(Float.parseFloat(alAppRating.get(position).toString()));






            return (row);
        }

在这里你可以看到带有onclicklistner的按钮holder.btnFavrowOpen我在那条线上得到了空指针非常多地绑在这里发生的事情....如果我在适配器中移除按钮代码它会正常工作......但是当我再次编写按钮代码,它获得空指针错误

1 个答案:

答案 0 :(得分:1)

使用

base.findViewById

而不是

base.findViewWithTag