0

我正在开发一个应用程序,我在其中CustomListview使用一个图标、raitingbar、复选框来实现。现在一切正常,但是当我插入一个按钮时,它让我出现 nullpointer 错误。我知道 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);

  }
}

自定义适配器

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);
        }

在这里,您可以看到holder.btnFavrowOpen带有 onclicklistner 的按钮,我在该行得到了空指针,这与此处发生的情况非常相关....如果我在适配器中删除按钮代码,它会正常工作...但是当我再次编写按钮代码时,它会出现错误空指针

4

1 回答 1

1

利用

base.findViewById

并不是

base.findViewWithTag 
于 2012-08-24T05:29:51.327 回答