哎呀!!
我有一个列表视图,应该用数据库中的数据填充
代码:

private void onCreateDBAndDBTabled() {
        myDB = this.openOrCreateDatabase(MY_DB_NAME, MODE_PRIVATE, null);
        myDB.execSQL("CREATE TABLE IF NOT EXISTS " + MY_DB_TABLE
                + " (_id integer primary key autoincrement, name varchar(100), rate integer(1), eattime datetime)"
                +";");
    ArrayList<Pizza> list = new ArrayList<Pizza>();
Cursor cursor = this.myDB.query(MY_DB_TABLE, new String[] { "name" },null,null,null,null,null,null);
      if (cursor.moveToFirst()) {
         do {
             Log.e("XXX", "Courser Enter: " + cursor.getString(0));
             Pizza pizza = new Pizza();
             pizza.title= cursor.getString(0);
            list.add(pizza);
            }
         while (cursor.moveToNext());
      }
      if (cursor != null && !cursor.isClosed()) {
         cursor.close();
      }
      Log.e("XXX", "Count:" + list.size());
      Log.e("XXX", "Item[0] -->" +  list.toArray()[0].toString());
      CustomAdapter aa = new CustomAdapter(this, R.layout.customlistitem,list);
     Log.e("XXX", String.valueOf(aa.getCount()));
      lv.setAdapter(aa);
    }

自定义适配器:
public class CustomAdapter extends ArrayAdapter<Pizza>{

    private ArrayList<Pizza> pizzen;

    public CustomAdapter(Context context, int textViewResourceId,ArrayList<Pizza> objects) {
        super(context, textViewResourceId, objects);
        this.pizzen = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        Pizza pizza = pizzen.get(position);
        if (v == null) {

        }

        return v;//super.getView(position, convertView, parent);


    }

披萨:
public class Pizza{

    public String title;
    public int rate;
    public Date date;
}

错误:
09-30 08:58:38.661: ERROR/XXX(667): Courser Enter: EditText
09-30 08:58:38.671: ERROR/XXX(667): Count:1
09-30 08:58:38.671: ERROR/XXX(667): Item[0] -->com.korn.pizzacounter.Pizza@43b91850
09-30 08:58:38.671: ERROR/XXX(667): 1
09-30 08:58:38.751: ERROR/AndroidRuntime(667): Uncaught handler: thread main exiting due to uncaught exception
09-30 08:58:38.762: ERROR/AndroidRuntime(667): java.lang.NullPointerException
09-30 08:58:38.762: ERROR/AndroidRuntime(667):     at android.widget.AbsListView.obtainView(AbsListView.java:1276)
09-30 08:58:38.762: ERROR/AndroidRuntime(667):     at android.widget.ListView.makeAndAddView(ListView.java:1668)
09-30 08:58:38.762: ERROR/AndroidRuntime(667):     at android.widget.ListView.fillDown(ListView.java:637)
09-30 08:58:38.762: ERROR/AndroidRuntime(667):     at android.widget.ListView.fillFromTop(ListView.java:694)
09-30 08:58:38.762: ERROR/AndroidRuntime(667):     at android.widget.ListView.layoutChildren(ListView.java:1521)
09-30 08:58:38.762: ERROR/AndroidRuntime(667):     at android.widget.AbsListView.onLayout(AbsListView.java:1113)
09-30 08:58:38.762: ERROR/AndroidRuntime(667):     at android.view.View.layout(View.java:6830)

虽然listsize为1,但错误为nullpoinerException(请参阅日志)

最佳答案

这是因为在您的getView()方法中,您没有对convertView进行充气,而是返回您的null
你的getView应该是这样的inflatingarow.xml包含一个TextView.

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        TextView mTextView = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.row, parent, false);
            mTextView = (TextView) convertView.findViewById(R.id.txt_list);
            convertView.setTag(mTextView);
        }
        else{
            mTextView = (TextView) convertView.getTag();
        }
        mTextView.setText(arrList.get(position));
        return convertView;
    }

07-24 17:01