如何修复android中的适配器中的空对象引用

时间:2015-12-05 09:08:38

标签: android android-listview android-arrayadapter

public class ListViewAdapter extends ArrayAdapter<DataModel> {
    // Declare Variables
    Context context;
    LayoutInflater inflater;
    List<DataModel> datamodel;
    private SparseBooleanArray mSelectedItemsIds;
    String producttype;
    MyDatabaseHelper dbManager;
    int counter;
    int Quantity = 0;
    HashMap<Integer, Integer> hashMap;

    static final String TAG = "LISTT";

    public ListViewAdapter(Context context, int resourceId,
                           List<DataModel> worldpopulationlist, String Type) {
        super(context, resourceId, worldpopulationlist);
        mSelectedItemsIds = new SparseBooleanArray();
        this.context = context;
        this.datamodel = worldpopulationlist;
        this.producttype = Type;
        inflater = LayoutInflater.from(context);


        hashMap = new HashMap<Integer, Integer>();
    }

    private class ViewHolder {
        TextView tv;
        TextView Quantity;
        TextView cost;
        ImageView img;
        ImageView plusitem;
        ImageView minusitem;
        TextView itemnumber;
    }

    @Override
    public int getCount() {
        return super.getCount();
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.listitem, null);
            //
            dbManager = new MyDatabaseHelper(context);
            holder.tv = (TextView) view.findViewById(R.id.textView1);
            holder.img = (ImageView) view.findViewById(R.id.imageView1);
            holder.Quantity = (TextView) view.findViewById(R.id.textView3);

            holder.cost = (TextView) view.findViewById(R.id.textView2);


            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Capture position and set to the TextViews
        String rupee = context.getResources().getString(R.string.Rs);
//        final MyDatabaseHelper dbManager = new MyDatabaseHelper(context);
        dbManager.open();

        holder.tv.setText(datamodel.get(position).getProductname());
        holder.Quantity.setText(datamodel.get(position)
                .getProdcutQuantity());
        holder.cost.setText(rupee + datamodel.get(position).getProdcutCost());
        //holder.img.setImageResource(datamodel.get(position).getProductimage());
//        dbManager.open();
        String imgae = dbManager.GetimageUrl(datamodel.get(position).getProdcutid());
        String imageUrl = "http://bhaskarmart.com/Images/" + imgae;
        Picasso.with(context).load(imageUrl).into(holder.img);
        holder.plusitem = (ImageView) view.findViewById(R.id.imageButton2);
        holder.plusitem.setTag(position);
        holder.minusitem = (ImageView) view.findViewById(R.id.imageButton);
        holder.minusitem.setTag(position);
        holder.itemnumber = (TextView) view.findViewById(R.id.textView4);


        holder.img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                imagealert(position);
            }
        });


//        dbManager.open();

        holder.itemnumber.setText(dbManager.GetEachShopedItemCount(datamodel
                .get(position).getProdcutid()));
        // holder.itemnumber.setText("5");
        holder.plusitem.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                int index = Integer.parseInt(arg0.getTag().toString());
                Log.e(TAG, Integer.toString(index));

                int quantity;

                if (hashMap.containsKey(index)) {
                    quantity = hashMap.get(index);
                } else
                    quantity = 0;
                quantity++;

                hashMap.put(index, quantity);
//                dbManager.open();
                if (dbManager.GetEachShopedItemCount(
                        datamodel.get(position).getProdcutid()).equals("")) {
                    holder.itemnumber.setText(quantity + "");
//                    dbManager.open();
                    dbManager.AddShopingItem(datamodel.get(position)
                                    .getProdcutid(), producttype,
                            datamodel.get(position).getProductname(), datamodel
                                    .get(position).getProdcutQuantity(),
                            datamodel.get(position).getProdcutCost(), String
                                    .valueOf(datamodel.get(position)
                                            .getProductimage()), String
                                    .valueOf(quantity), "true");

                } else {

//                    dbManager.open();
                    int dbvalue = Integer.parseInt(dbManager
                            .GetEachShopedItemCount(datamodel.get(position)
                                    .getProdcutid()));
                    int totalvaue = dbvalue;
                    totalvaue++;
                    holder.itemnumber.setText(totalvaue + "");
//                    dbManager.open();
                    dbManager.AddShopingItem(datamodel.get(position)
                                    .getProdcutid(), producttype,
                            datamodel.get(position).getProductname(), datamodel
                                    .get(position).getProdcutQuantity(),
                            datamodel.get(position).getProdcutCost(), String
                                    .valueOf(datamodel.get(position)
                                            .getProductimage()), String
                                    .valueOf(totalvaue), "true");

                }

                // holder.itemnumber.setText(quantity + "");
                // holder.itemnumber.invalidate();
                // holder.itemnumber
                // .setText(datamodel.get(position).getEachProdcutcount());

                sendBroadcaset(true);

            }

        });
        holder.minusitem.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                int index = Integer.parseInt(v.getTag().toString());
                Log.e(TAG, Integer.toString(index));

                int quantity;

                if (hashMap.containsKey(index)) {
                    quantity = hashMap.get(index);
                } else
                    quantity = 0;

                quantity--;

                if (quantity < 0)
                    quantity = 0;

                hashMap.put(index, quantity);
                // holder.itemnumber.setText(quantity + "");
                // holder.itemnumber.invalidate();
                // holder.itemnumber
                // .setText(datamodel.get(position).getEachProdcutcount());
//                dbManager.open();
                if (dbManager.GetEachShopedItemCount(
                        datamodel.get(position).getProdcutid()).equals("")) {
                    holder.itemnumber.setText(quantity + "");
                    dbManager.AddShopingItem(datamodel.get(position)
                                    .getProdcutid(), producttype,
                            datamodel.get(position).getProductname(), datamodel
                                    .get(position).getProdcutQuantity(),
                            datamodel.get(position).getProdcutCost(), String
                                    .valueOf(datamodel.get(position)
                                            .getProductimage()), String
                                    .valueOf(quantity), "true");

                } else {
//                    dbManager.open();
                    int dbvalue = Integer.parseInt(dbManager
                            .GetEachShopedItemCount(datamodel.get(position)
                                    .getProdcutid()));
                    int totalvaue = dbvalue + quantity;
                    int nexttotvalue;
                    if (totalvaue > 0) {
                        nexttotvalue = --totalvaue;

//                        Toast.makeText(context, String.valueOf(nexttotvalue),
//                                10000).show();

                    } else {
                        nexttotvalue = 0;
                    }
                    // if (totalvaue < 0)
                    // totalvaue = 0;

                    holder.itemnumber.setText(totalvaue + "");
//                    dbManager.open();
                    dbManager.AddShopingItem(datamodel.get(position)
                                    .getProdcutid(), producttype,
                            datamodel.get(position).getProductname(), datamodel
                                    .get(position).getProdcutQuantity(),
                            datamodel.get(position).getProdcutCost(), String
                                    .valueOf(datamodel.get(position)
                                            .getProductimage()), String
                                    .valueOf(totalvaue), "true");

                }

                sendBroadcaset(false);
            }
        });
        return view;
    }

这是我的代码我在Fragment ListView中使用这个适配器我有5个选项卡每个选项卡都与片段集成,每个片段我使用listView适配器当我从选项卡1转到选项卡2然后当我来时没有异常来从选项卡5返回到选项卡2然后在适配器中出现此异常

 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference

和例外

 at info.androidhive.materialdesign.adapter.ListViewAdapter.<init>(ListViewAdapter.java:43)

这是这一行:  super(context, resourceId, worldpopulationlist);

请告诉我如何解决此异常建议我

1 个答案:

答案 0 :(得分:1)

试试这个

listviewadapter = new ListViewAdapter(getActivity(), R.layout.listitem, lstDataModel, "2");

而不是

listviewadapter = new wholeandflavoradapter(getActivity(), R.layout.listitem, lstDataModel, "2");