基础适配器上的零点异常

时间:2015-05-19 07:41:17

标签: android listview android-listview nullpointerexception baseadapter

我正在使用带有基本适配器的自定义列表视图。当我在listview上设置适配器时,它给出了零点异常。我正在检查空值,但它仍然给出异常。

这是我的检查点。

    if(product_title.size() > 0 && product_quantity.size() > 0){

        materialList.setAdapter(new MaterialListAdapter(MaterialList.this, product_title,product_quantity));

    }

这是我的全班

public class MaterialList extends Activity{

ListView materialList;
List<Book> list;
private List<String> product_title = new ArrayList<String>();
private List<String> product_quantity = new ArrayList<String>();
JCGSQLiteHelper db;
Book selectedProduct;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.material_list);


    ActionBar actionBar = getActionBar();
    actionBar.hide();
    db = new JCGSQLiteHelper(getApplicationContext());

    list = db.getAllBooks();
    for (int i = 0; i < list.size(); i++) {
        product_title.add(i, list.get(i).getTitle());
        product_quantity.add(i, list.get(i).getAuthor());
        }

    if(product_title.size() > 0 && product_quantity.size() > 0){

        materialList.setAdapter(new MaterialListAdapter(MaterialList.this, product_title,product_quantity));

    }



}
public class MaterialListAdapter extends BaseAdapter{

       List<String> title = new ArrayList<String>();
       List<String> quantity = new ArrayList<String>();
       Context context;
          private LayoutInflater inflater=null;
        public MaterialListAdapter(MaterialList mainActivity, List<String> catalog_title, List<String> catalog_quantity  ) {
            // TODO Auto-generated constructor stub
             context=mainActivity;
            title =catalog_title;

            quantity = catalog_quantity;
             inflater = ( LayoutInflater )context.
                     getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return title.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public class ViewHolder
        {
            TextView Title, Quanity, remove ;


        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder;
            holder =new ViewHolder();
            View rowView;

                 rowView = inflater.inflate(R.layout.cust_material_lis_item, null);
                 holder.Title=(TextView) rowView.findViewById(R.id.product);
                 holder.Quanity=(TextView) rowView.findViewById(R.id.quantity);
                 holder.remove=(TextView) rowView.findViewById(R.id.remove);

             holder.Title.setText(title.get(position));
             holder.Quanity.setText(quantity.get(position));
             holder.remove.setText("Remove");


             holder.remove.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int id = list.get(position).getId();
                    selectedProduct =db.readBook(id);
                    db.deleteBook(selectedProduct);
                }
            });

            return rowView;
        }

}


}

这是我的适配器

public class MaterialListAdapter extends BaseAdapter{

       List<String> title = new ArrayList<String>();
       List<String> quantity = new ArrayList<String>();
       Context context;
          private LayoutInflater inflater=null;
        public MaterialListAdapter(MaterialList mainActivity, List<String> catalog_title, List<String> catalog_quantity  ) {
            // TODO Auto-generated constructor stub
             context=mainActivity;
            title =catalog_title;

            quantity = catalog_quantity;
             inflater = ( LayoutInflater )context.
                     getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return title.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public class ViewHolder
        {
            TextView Title, Quanity, remove ;


        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder;
            holder =new ViewHolder();
            View rowView;

                 rowView = inflater.inflate(R.layout.cust_material_lis_item, null);
                 holder.Title=(TextView) rowView.findViewById(R.id.product);
                 holder.Quanity=(TextView) rowView.findViewById(R.id.quantity);
                 holder.remove=(TextView) rowView.findViewById(R.id.remove);

             holder.Title.setText(title.get(position));
             holder.Quanity.setText(quantity.get(position));
             holder.remove.setText("Remove");

             holder.remove.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int id = list.get(position).getId();
                    selectedProduct =db.readBook(id);
                    db.deleteBook(selectedProduct);
                }
            });

            return rowView;
        }

}

这一行给出零点异常

materialList.setAdapter(new MaterialListAdapter(MaterialList.this, product_title,product_quantity));

2 个答案:

答案 0 :(得分:3)

你宣布了materialList,但是你忘记了初始化它。添加

materialList = findViewById(R.id...) 
setContentView

之后

答案 1 :(得分:2)

  

materialList =空

所以initialize之前使用

materialList = findViewById(R.id.listViewID);