我想知道是否有人可以帮我弄清楚为什么我在调用下面的方法时会得到一个空对象引用。我正在展示我的dbhelper类和我的listview适配器类。基本上是listview适配器的一部分,它处理我的radiogroup onCheckedChanged然后将数据发送到sqlite数据库。
DBHelper类:
public void addListItem(String carmodel,String Condition, String x) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBHelper.Model, carmodel);
values.put(DBHelper.Condition, Condition);
values.put(DBHelper.UPDATETIME, x);
db.insert(TABLE_CARFACT, null, values);
db.close();
}
我的listview适配器类如下所示。空对象引用错误发生在我调用db.addListItem(....)的底部。任何帮助,将不胜感激。
public class ListCarsAdapter extends BaseAdapter {
private SQLiteDatabase uDatabase;
private DBHelper uDbHelper;
public static final String TAG = "ListCarsAdapter";
private List<Car> mItems;
private LayoutInflater mInflater;
private DBHelper db;
public ListCarsAdapter(Context context, List<Car> listCars) {
this.setItems(listCars);
this.mInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return (getItems() != null && !getItems().isEmpty()) ? getItems().size() : 0 ;
}
@Override
public Car getItem(int position) {
return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position) : null ;
}
@Override
public long getItemId(int position) {
return (getItems() != null && !getItems().isEmpty()) ? getItems().get(position).getId() : position;
}
@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
if(v == null) {
v = mInflater.inflate(R.layout.list_item_car, parent, false);
holder = new ViewHolder();
holder.txtCarName = (TextView) v.findViewById(R.id.txt_car_name);
holder.radioGroup = (RadioGroup) v.findViewById(R.id.scale);
holder.radioGroup.setTag(position);
v.setTag(holder);
}
else {
holder = (ViewHolder) v.getTag();
}
// fill row data
final Car currentItem = getItem(position);
if(currentItem != null) {
holder.txtCarName.setText(currentItem.getCar());
holder.radioGroup.setTag(currentItem.getAnswer());
}
holder.radioGroup.setOnCheckedChangeListener(null);
holder.radioGroup.clearCheck();
if(checked.indexOfKey(position)>-1){
holder.radioGroup.check(checked.get(position));
}else{
holder.radioGroup.clearCheck();
}
holder.radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String Answer = null;
if(checkedId>-1){
checked.put(position, checkedId);
switch (checkedId) {
case R.id.a1:
Answer = "r1";
break;
case R.id.a2:
Answer = "r2";
break;
case R.id.a3:
Answer = "r3";
break;
case R.id.a4:
Answer = "r4";
break;
}
db.addListItem("testCar", Answer, getCurrentTimeStamp()); /*<--null object reference happening here*/
}else{
if(checked.indexOfKey(position)>-1)
checked.removeAt(checked.indexOfKey(position));
}
}
});
return v;
}
public List<Car> getItems() {
return mItems;
}
public void setItems(List<Car> mItems) {
this.mItems = mItems;
}
class ViewHolder {
TextView txtCarName;
protected RadioGroup radioGroup;
}
public static String getCurrentTimeStamp(){
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTimeStamp = dateFormat.format(new Date()); // Find todays date
return currentTimeStamp;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
答案 0 :(得分:1)
问题是private DBHelper db;
只是定义了但我在你的代码中没有看到你正在创建该类的对象。请在ListCarsAdapter
中的某个位置安装该课程,例如:
private DBHelper db;
public ListCarsAdapter(Context context, List<Car> listCars) {
this.setItems(listCars);
this.mInflater = LayoutInflater.from(context);
db = new DBHelper();//<--your awesome object of DBHelper class
}