Android:arrayAdapter抛出空指针异常?

时间:2015-04-28 21:55:48

标签: android android-arrayadapter

我遇到了阵列适配器的一些问题。

这就是我的lessonNotesList类:

     mydb = new DBHelper(this);
    //Creating an ArrayList of contacts data
    ArrayList array_list = mydb.getAllLessonNotes();
    //Creating an Array Adapter to manipulate the contacts data into a ListView
    ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.list_layout, array_list);
    //Adding the contacts to the list view.
    lessonNote = (ListView)findViewById(R.id.listView1);
    lessonNote.setAdapter(arrayAdapter);
    //Setting an onClickListener to process the user's actions
    lessonNote.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub

            //Prepare the data from the ListView item to be passed to new activity
            //arg2 is increased by one as its a zero based index but the contacts table in the database works of a base index of 1

            int id_To_Search = VALUE;
            //Bundle extras = getIntent().getExtras();
            Bundle dataBundle = new Bundle();
            dataBundle.putInt("id", id_To_Search);
            Log.d("Id is ^&*: ", String.valueOf(id_To_Search));
            //Create a new intent to open the DisplayContact activity
            Intent intent = new Intent(getApplicationContext(), com.example.ltss.dyslexia.app.LessonNotes.class);
            intent.putExtras(dataBundle);
            startActivity(intent);
        }
    });

DBHelper类:

public ArrayList getAllLessonNotes()
{
    ArrayList array_list = new ArrayList();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from "+TABLE_LESSON_NOTES, null);
    res.moveToFirst();
    while(res.isAfterLast() == false)
    {
        array_list.add(res.getString(res.getColumnIndex(DATE)));
        res.moveToNext();
    }
    return array_list;
}

代码在这里遇到NULL指针异常崩溃:

lessonNote.setAdapter(arrayAdapter);

为什么会发生这种情况? 我已经尝试过调试,调试器告诉我arrayAdatper包含1个条目

对于不同的页面我有几乎相同的代码,并且它的工作正常100%。

欢迎任何建议!

谢谢

1 个答案:

答案 0 :(得分:1)

你确定这条线吗?

lessonNote = (ListView)findViewById(R.id.listView1);

似乎在您的布局XML中,没有视图具有“listView1”id。