从Android ListView删除最后一项崩溃该应用程序

时间:2017-01-01 00:59:36

标签: android listview custom-adapter

这是我得到的错误:

Process: com.painlessshopping.mohamed.findit, PID: 21324
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference
        at android.support.design.widget.Snackbar.<init>(Snackbar.java:192)
        at android.support.design.widget.Snackbar.make(Snackbar.java:224)
        at com.painlessshopping.mohamed.findit.CartAdapter$1.onClick(CartAdapter.java:100)
        at android.view.View.performClick(View.java:5201)
        at android.view.View$PerformClick.run(View.java:21163)
        at android.os.Handler.handleCallback(Handler.java:746)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5443)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

以下是我的CartAdapter类的代码:

public class CartAdapter extends ArrayAdapter<Item> implements View.OnClickListener{

    private ArrayList<Item> dataSet;
    Context mContext;

    // View lookup cache
    private static class ViewHolder {
        TextView itemTitle;
        TextView itemStore;
        TextView itemPrice;
        ImageView storeLogo;
        ImageView removeFromCart;
    }

    public CartAdapter (ArrayList<Item> data, Context context) {
        super(context, R.layout.row_cart, data);
        this.dataSet = data;
        this.mContext=context;

    }

    @Override
    public void onClick(View v) {

        int position=(Integer) v.getTag();
        Object object= getItem(position);
        final Item item=(Item)object;

        CharSequence actions[];

        final View view = v;
        switch (v.getId())
        {

            case R.id.more:
                System.out.println(item.getLink());
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(item.getLink()));
                mContext.startActivity(browserIntent);
                break;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Get the data item for this position
        final Item item = getItem(position);

        // Check if an existing view is being reused, otherwise inflate the view
        ViewHolder viewHolder; // view lookup cache stored in tag

        final View result;


            viewHolder = new ViewHolder();
            LayoutInflater inflater = LayoutInflater.from(getContext());
            convertView = inflater.inflate(R.layout.row_cart, parent, false);
            viewHolder.itemTitle = (TextView) convertView.findViewById(R.id.title);
            viewHolder.itemStore = (TextView) convertView.findViewById(R.id.retailer);
            viewHolder.itemPrice = (TextView) convertView.findViewById(R.id.price);
            viewHolder.storeLogo = (ImageView) convertView.findViewById(R.id.more);
            viewHolder.removeFromCart = (ImageView) convertView.findViewById(R.id.removeCart);

            result = convertView;

            convertView.setTag(viewHolder);

        viewHolder.itemTitle.setText(item.getTitle());
        viewHolder.itemStore.setText("From " + item.getStore());
        viewHolder.itemPrice.setText("$" + item.getPrice());
        viewHolder.storeLogo.setOnClickListener(this);

        viewHolder.removeFromCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CartInfoProvider.removeFromCart(item);
                MyCart.adapter.remove(item);
                MyCart.adapter.notifyDataSetChanged();

                Snackbar.make(result, "This item was removed from your cart", Snackbar.LENGTH_INDEFINITE).setAction("UNDO", new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        CartInfoProvider.addToCart(item);
                        MyCart.adapter.add(item);
                        MyCart.adapter.notifyDataSetChanged();
                    }

                }).show();

            }
        });


        viewHolder.storeLogo.setTag(position);

        // Return the completed view to render on screen
        return convertView;

    }
}

以下是onCreate的{​​{1}}:

CartActivity

当购物车中有多个商品时,此功能可以正常运行。但是当我尝试从购物车中删除 FINAL 项时,应用程序崩溃并抛出上述错误。有任何想法吗?我不知道出了什么问题。

编辑:我假设有人会要求我提供protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_cart); ListView listView=(ListView)findViewById(R.id.cart); adapter= new CartAdapter(Items, this); listView.setAdapter(adapter); TextView empty = (TextView) findViewById(R.id.empty); listView.setEmptyView(empty); adapter.addAll(CartInfoProvider.getCart()); adapter.notifyDataSetChanged(); } 的代码,所以这里是:

CartInfoProvider

1 个答案:

答案 0 :(得分:0)

正如@CommonsWare所说,我应该一直使用listView而不是实际的行显示小吃吧。这是因为当我删除行时,snackbar正试图将自己附加到不存在的东西上,因此就是nullpointer。

为了解决这个问题,我只是将我的适配器的构造函数更改为在listview中读取,然后将其用于我的快餐栏。干杯!