Why does populating a <ListView> from SQL crashes my Android activity?-Collection of common programming errors

I am trying to do an example from the Android Developer’s Cookbook to create and populate an ListView with entries from a SQLite file.

Unfortunately I can not find the bug in my code. The activity is crashing right after I reference the R.layout.diaries. Can anyone tell me where my problem is, and why I am crashing with my layout?

Java file:

public class DisplayDiaries extends ListActivity{
    MyDB dba;
    DiaryAdapter myAdapter;

    private class MyDiary { 
        public MyDiary(String t, String c, String r){
            title = t;
            content = c;
            recorddate= r;
        }
        public String title;
        public String content;
        public String recorddate;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        Log.i("DisplayDiaries", "started onCreate");
        dba = new MyDB(this);
        dba.open();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.diaries);
    //I am crashing here
        Log.i("DisplayDiaries", "layout complite");

        myAdapter = new DiaryAdapter(this);
        this.setListAdapter(myAdapter);
    }
    private class DiaryAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private ArrayList diaries;
        public DiaryAdapter (Context context) {
            mInflater = LayoutInflater.from(context);
            diaries = new ArrayList();
            getdata();
        }
        public void getdata(){
            Cursor c = dba.getdiaries();
            startManagingCursor(c);
            if(c.moveToFirst()){
                do{
                    String title = c.getString(c.getColumnIndex(Constants.TITLE_NAME));
                    String content = c.getString(c.getColumnIndex(Constants.CONTENT_NAME));
                    DateFormat dateFormat = DateFormat.getDateTimeInstance();
                    String datedata = dateFormat.format(new Date(c.getLong(c.getColumnIndex(Constants.DATE_NAME))).getTime());
                    MyDiary temp = new MyDiary(title,content,datedata);
                    diaries.add(temp);
                }while(c.moveToNext());
            }
        }

        @Override
        public int getCount() {
            return diaries.size();
        }

        @Override
        public MyDiary getItem(int i) {
            return diaries.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            final ViewHolder holder;
            View v = arg1;
            if ((v==null) || (v.getTag() == null)) {
                v = mInflater.inflate(R.layout.diaryrow, null);
                holder = new ViewHolder();
                holder.mTitle =(TextView)v.findViewById(R.id.name);
                holder.mDate = (TextView)v.findViewById(R.id.datetext);
                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }

            holder.mdiary = getItem(arg0);
            holder.mTitle.setText(holder.mdiary.title);
            holder.mDate.setText(holder.mdiary.recorddate);

            v.setTag(holder);
            return v;
        }

        public class ViewHolder {
            MyDiary mdiary;
            TextView mTitle;
            TextView mDate;
        }
    }
}

diaries.xml file:






diaryrow.xml file:


    
    
    
         

  1. I am guessing that it bombs out because you have commented out the TextView in diaries.xml, which should by the way have an ID of @android:id/empty.

  2. Hey even i have been working on the same database model… even i had to face the same problem…wherein it couldn’t resolve R.layout.history Note: history.xml file has the same content as the dairies.xml file just the name is different

    what i did was i recreated the same file with the same content… by right clicking the layout folder–>new–>Android XML File

    Then again while entering the path(as in R.layout.history in my case) were initially the error was occuring… try to go step by step by typeing in R.layout. and then let the eclipse drop box appear then select the file dairies…

    if you still cant get it then try checking your R.java(do no modify it!).

  3. The ID for the ListView in diaries.xml should be @android:id/list (Also, in diaryrow.xml, you should move android:layout_alignLeft and android:layout_below from the RelativeLayout element to the second TextView element)

  4. I’m not sure if this is it, but in my listview activity xml, the id of the listview is:

    android:id="@android:id/list"
    

    and not

    android:id="@+id/list"
    

    I’m really new to android, but could that be it?