使用arrayadapter的nullpointerexception

时间:2014-07-09 00:40:11

标签: android-listview nullpointerexception

当我使用arrayadapter时,我得到空指针异常。

错误说

"在android.widget.arrayadapter.getcount  在android.widget.listview.setAdapter"

我不知道为什么会收到此错误。

我会简化我的代码。

请告诉我为什么会收到此错误。

公共类ChapterDisease扩展了Activity

 implements OnItemClickListener,
        OnItemLongClickListener {

    /* Setting Database */
    SQLiteDatabase db = MainActivity.db;
    Cursor cursor;

    /* Initializing */
    ListView listview;
    /* Get the chapter disease list */
    ArrayList<Disease> diseaseList;
    /* The list to show the disease names in the activity UI */
    ArrayList<String> diseaseNamesList;

    ArrayAdapter<String> adapter;
    private ChapterDiseaseAdapter rowAdapter = null;

我宣布listview和arraylist。

我的适配器是行适配器,而ChapterDiseaseAdapter是内部类。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

rowAdapter = new ChapterDiseaseAdapter();

listview = (ListView) findViewById(R.id.chap_listView);
        listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listview.setOnItemClickListener(this);
        listview.setOnItemLongClickListener(this);
        System.out.println("size of diseaseNamesList ="+diseaseNamesList.size());
        listview.setAdapter(rowAdapter);

这是onCreate。

我试图打印数组的大小。这是21,而不是0。

    class ChapterDiseaseAdapter extends ArrayAdapter<String> {

        ChapterDiseaseAdapter() {
            super(ChapterDisease.this,
                    R.layout.activity_chapter_disease_row,diseaseNamesList);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView == null)
                convertView = super.getView(position, convertView, parent);
            View row = convertView; 
            DiseaseHolder holder = null;

            if (row == null) {
                LayoutInflater inflater = getLayoutInflater();

                row = inflater.inflate(R.layout.activity_chapter_disease_row,
                        parent, false);
                holder = new DiseaseHolder(row, order++);
                row.setTag(holder);
            } else {
                holder = (DiseaseHolder) row.getTag();
            }

            holder.populateFrom(diseaseNamesList.get(position));

            return (row);
        }
    }

    // This class is the holder of listview1
    class DiseaseHolder {

        private ImageView imgView = null;
        private TextView txt = null;
        private TextView level = null;
        private ImageView icon = null;

        private CheckBox check = null;

        DiseaseHolder(View row, int order) {
            imgView = (ImageView) row.findViewById(R.id.chapter_disease_row_img);

            txt = (TextView) row.findViewById(R.id.chapter_disease_row_txt);


        }

        void populateFrom(String d) {
            imgView.setImageResource(R.drawable.ico1);
            txt.setText(d);
//          name.setText(r.getMonster().getMonsterName());
//          level.setText("" + r.getMonster().getMonsterLevel());
//          icon.setImageResource(r.getMonster().getImgMarker());

        }
    }
}

最后是内部阶级。

1 个答案:

答案 0 :(得分:0)

我认为您收到此错误是因为在ChapterDiseaseAdapter类中diseaseNamesList对象为空,因为您尚未将其传递给适配器。构造函数。您还需要将活动上下文传递给适配器。

首先在你的onCreate方法中:

rowAdapter = new ChapterDiseaseAdapter(this, diseaseNamesList);

然后在ChapterDiseaseAdapter.java中:

ChapterDiseaseAdapter(Context context, List<String> diseaseNamesList) {
    super(context, R.layout.activity_chapter_disease_row, diseaseNamesList);
}