基本适配器上的空点异常
null point exception on base Adapter
我正在使用带有基本适配器的自定义列表视图。当我在列表视图上设置适配器时,它会给出空点异常。我正在检查空值,但它仍然给出异常。
这是我的检查点。
if(product_title.size() > 0 && product_quantity.size() > 0){
materialList.setAdapter(new MaterialListAdapter(MaterialList.this, product_title,product_quantity));
}
这是我的全class
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));
materialList=null
所以initialize
它在使用之前
materialList = findViewById(R.id.listViewID);
您声明了您的 materialList
,但您忘记了初始化它。添加
materialList = findViewById(R.id...)
在setContentView
之后
我正在使用带有基本适配器的自定义列表视图。当我在列表视图上设置适配器时,它会给出空点异常。我正在检查空值,但它仍然给出异常。
这是我的检查点。
if(product_title.size() > 0 && product_quantity.size() > 0){
materialList.setAdapter(new MaterialListAdapter(MaterialList.this, product_title,product_quantity));
}
这是我的全class
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));
materialList=null
所以initialize
它在使用之前
materialList = findViewById(R.id.listViewID);
您声明了您的 materialList
,但您忘记了初始化它。添加
materialList = findViewById(R.id...)
在setContentView