Java db fetch上OnClick()内的NullPointerException

Java db fetch上OnClick()内的NullPointerException,java,android,nullpointerexception,simplecursoradapter,Java,Android,Nullpointerexception,Simplecursoradapter,在尝试刷新视图中的数据时,在自定义simplecursoradapter中找出NullPointerException。当使用SimpleCursorAdapter时,您不能使用NotifyDataSetChanged(),因此我需要创建一个新的适配器,并且在传递所需的数据时遇到困难 public class DxSimpleCursorAdapter extends SimpleCursorAdapter { Context context; Activity activity; DxDbAd

在尝试刷新视图中的数据时,在自定义simplecursoradapter中找出NullPointerException。当使用SimpleCursorAdapter时,您不能使用NotifyDataSetChanged(),因此我需要创建一个新的适配器,并且在传递所需的数据时遇到困难

public class DxSimpleCursorAdapter extends SimpleCursorAdapter {
Context context;
Activity activity;
DxDbAdapter dbh;
DxSimpleCursorAdapter adapter;
ListView lv;

protected String subcategory;

public DxSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, String param) {
    super(context, layout, c, from, to);
    this.context=context;
    this.activity=(Activity) context;
    subcategory = param;
}

public DxSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    this.context=context;
    this.activity=(Activity) context;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View row = inflater.inflate(R.layout.list_detail, null);
    ViewHolder holder = new ViewHolder();
    holder.image = (ImageView) row.findViewById(R.id.fav);
    holder.diagnosis = (TextView) row.findViewById(R.id.diagnosis);
    holder.code = (TextView) row.findViewById(R.id.code);
    row.setTag(holder);
    return row;
}

class ViewHolder {
    ImageView image;
    TextView diagnosis;
    TextView code;      
}

class Status {
    int status;
    Long id;
}

@Override
public void bindView(View v, Context con, Cursor cursor) {
    ViewHolder holder = (ViewHolder) v.getTag();

    int favstatus = cursor.getInt(cursor.getColumnIndex(DxDbAdapter.FAV));
    Status state = new Status();

    if (favstatus == 1) {
        holder.image.setImageResource(R.drawable.btn_star_on_normal);
        state.status = 1;
    }
    else if (favstatus == 0) {
        holder.image.setImageResource(R.drawable.btn_star_off_normal);
        state.status = 0;
    }

    long id = cursor.getLong(cursor.getColumnIndex(DxDbAdapter.DIAG_ID));
    state.id = id;
    holder.image.setTag(state);
    holder.diagnosis.setText(cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG)));
    holder.code.setText(cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG_CODE)));

    holder.image.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Status current_state = (Status) v.getTag();

            ImageView fav = (ImageView) v.findViewById(R.id.fav);
            fav.setImageResource(R.drawable.ic_fav);

            if (current_state.status == 1) {
                Toast toast = Toast.makeText(context,"Update fav status to 1",Toast.LENGTH_SHORT);
                toast.show();
            }
            else if (current_state.status == 0) {
                Toast toast = Toast.makeText(context,"Update fav status to 0",Toast.LENGTH_SHORT);
                toast.show();
            }

                    dbh = new DxDbAdapter(context);
            dbh.open();
            Cursor newCursor = dbh.fetch(1, subcategory);
            String[] columns = new String[] {"diagnosis", "diagcode"};
            int[] to = new int[] {R.id.diagnosis, R.id.code};
            adapter = new DxSimpleCursorAdapter(context, R.layout.list_detail, newCursor, columns, to);
            lv.setAdapter(adapter);
        }
    });
    String diag = cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG));
    String code = cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG_CODE));
    holder.diagnosis.setText(diag);
    holder.code.setText(code);
}

问题在于subcategory=getIntent().getStringExtra(“subcategory”)和游标newCursor=dbh.fetch(1,子类别)并且我必须引用正在使用适配器的活动。我想我在引用活动的正确语法方面遇到了问题,这样我就不会得到NullPointerException

您确实应该从父活动传入子类别,因为使用适配器时此值不会更改(而不是每次绑定到行时都检索它)

例:在家长活动中,你会做如下事情

String subcategory = getIntent().getStringExtra("SUBCATEGORY"); 
DxSimpleCursorAdapter adapter = DxSimpleCursorAdapter(context, layout, c, from, to, subcategory);
listView.setAdapter(adapter); 

显然,您需要修改适配器的构造函数,使其接受该字符串,然后将其分配给适配器内的子类别变量

我添加了一个构造函数,该构造函数具有附加字段,调用DxSimpleCursorAdapter的ListActivity现在传递子类别值,但我仍然有一个问题在游标newCursor=dbh.fetch(1,子类别)上发生NullPointerException时;。fetch()方法在其他活动中也起作用,因此我不确定问题出在哪里,因为我对Java的了解有限。dbh看起来是空的。我在代码中没有看到分配或创建DxDbAdapter dbh对象的任何地方,我添加了dbh=newDXDBAdapter(上下文);胸径开放();到我上面的代码,现在我的NullPointerException已经转到lv.setAdapter(adapter);。有什么想法吗?我是否正确分配和创建了dbh对象?应该可以。为什么适配器中有listview变量?listview应该存在于主活动中。不在适配器中。可以这样想:activity持有listview->listview持有adapter->adapter持有要显示的项OOPS抱歉。尝试使用DxSimpleCursorAdapter.this.changeCursor(newCursor);您不需要创建新的适配器