我有一个名为“noteName”的列的数据库。我想要的是用mySQLiteDatabase填充listView。我使用显示适配器来帮助这个过程。
但问题是Null Pointer Exception。我必须弄清楚是什么导致NPO,但我仍然没有得到它。
请帮助我,我刚刚被困在这里两天。
NB。这是我的MainActivity.java代码(listview将显示的地方)
public class MainActivity extends ActionBarActivity {
SQLiteDatabase db;
DatabaseHelper dBHelper = new DatabaseHelper(this);
private ListView list;
private ArrayList<String> NoteName = new ArrayList<String>();
private static final String TABLE_NOTES_NAME = "Notes";
private ArrayList<Contact> noteData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.pink_icon).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent registerIntent = new Intent(MainActivity.this, AddNote.class);
MainActivity.this.startActivity(registerIntent);
}
});
display();
}
public void display() {
db = dBHelper.getReadableDatabase();
Cursor mCursor = db.rawQuery("SELECT NoteName FROM " + TABLE_NOTES_NAME , null);
ListView list = (ListView)findViewById(android.R.id.list);
NoteName.clear();
if (mCursor.moveToFirst()) {
do {
NoteName.add(mCursor.getString(mCursor.getColumnIndex(dBHelper.TOL_NOTENAME)));
} while (mCursor.moveToNext());
}
DisplayAdapterTrans disadptr = new DisplayAdapterTrans(MainActivity.this, NoteName);
list.setAdapter(disadptr); // Here the console says have a null pointer exception
mCursor.close();
}
}
这是我的DisplayAdapter
public class DisplayAdapterTrans extends BaseAdapter {
private Context mContext;
private ArrayList<String> NoteName;
public DisplayAdapterTrans(Context c, ArrayList<String> noteName) {
this.mContext = c;
this.NoteName = noteName;
}
public int getCount() {
// TODO Auto-generated method stub
return NoteName.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.notename, null);
mHolder = new Holder();
mHolder.noteName= (TextView) child.findViewById(R.id.notenameTxt);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.noteName.setText(NoteName.get(pos));
return child;
}
public class Holder {
TextView noteName;
}
}