Android:arrayAdapter 抛出空指针异常



我在使用阵列适配器时遇到了一些问题。

这是我的课程笔记列表类中的内容:

     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% 正常..

欢迎任何建议!

谢谢

你确定那条线吗?

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

似乎在您的布局 XML 中,没有视图具有"listView1"标识。

最新更新