SimpleCursorAdapter force close

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

SimpleCursorAdapter force close

Postby leslie » Sun Mar 22, 2009 8:01 pm

Hi!

I have code, and it got a force close all day. I dont know what is the problem, but when i pick up this 2 line its running (but i have not a list):
1. SimpleCursorAdapter sets = new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
2. setListAdapter(sets);

Here is the prgram:

The LoVoMa.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. ...
  3.  
  4. public class LoVoMa extends ListActivity{
  5.  
  6.  
  7.  
  8.         private DbAdapter mDbHelper;
  9.  
  10.         private Cursor c;      
  11.  
  12.        
  13.  
  14.         @Override
  15.  
  16.          public void onCreate(Bundle savedInstanceState) {
  17.  
  18.                 super.onCreate(savedInstanceState);
  19.  
  20.                 setContentView(R.layout.notes_list);
  21.  
  22.                 mDbHelper = new DbAdapter(this);
  23.  
  24.                 mDbHelper.open();
  25.  
  26.                 fillData();
  27.  
  28.                 registerForContextMenu(getListView());
  29.  
  30.             }
  31.  
  32.        
  33.  
  34.          private void fillData() {
  35.  
  36.                 // Get all of the rows from the database and create the item list
  37.  
  38.                 c = mDbHelper.fetchAllNotes();
  39.  
  40.                 startManagingCursor(c);
  41.  
  42.                
  43.  
  44.                 // Create an array to specify the fields we want to display in the list (only TITLE)
  45.  
  46.                 String[] from = new String[]{DbAdapter.KEY_MODEL};
  47.  
  48.                
  49.  
  50.                 // and an array of the fields we want to bind those fields to (in this case just text1)
  51.  
  52.                 int[] to = new int[]{R.id.text122};
  53.  
  54.                
  55.  
  56.                 // Now create a simple cursor adapter and set it to display
  57.  
  58.                 SimpleCursorAdapter sets = new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
  59.  
  60.                 //setListAdapter(sets);
  61.  
  62.             }...
  63.  
  64.  
  65.  
  66.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


The DbAdapter.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class DbAdapter {
  3.  
  4.  
  5.  
  6.     public static final String KEY_ID = "Id";
  7.  
  8.     public static final String KEY_MODEL = "Model";
  9.  
  10.     public static final String KEY_COMPANY = "Company";
  11.  
  12.     private static final String TAG = "DbAdapter";
  13.  
  14.     private DatabaseHelper mDbHelper;
  15.  
  16.     private SQLiteDatabase db;
  17.  
  18.        
  19.  
  20.     private static final String DATABASE_CREATE =
  21.  
  22.             "CREATE TABLE IF NOT EXISTS my_table (Id INTEGER PRIMARY KEY AUTOINCREMENT, Company TEXT, Model TEXT);";
  23.  
  24.  
  25.  
  26.     private static final String DATABASE_NAME = "myDB";
  27.  
  28.     private static final String DATABASE_TABLE = "my_table";
  29.  
  30.     private static final int DATABASE_VERSION = 2;
  31.  
  32.  
  33.  
  34.     private final Context mCtx;
  35.  
  36.  
  37.  
  38.     private static class DatabaseHelper extends SQLiteOpenHelper {
  39.  
  40.  
  41.  
  42.         DatabaseHelper(Context context) {
  43.  
  44.             super(context, DATABASE_NAME, null, DATABASE_VERSION);
  45.  
  46.         }
  47.  
  48.  
  49.  
  50.         @Override
  51.  
  52.         public void onCreate(SQLiteDatabase db) {
  53.  
  54.  
  55.  
  56.             db.execSQL(DATABASE_CREATE);
  57.  
  58.         }
  59.  
  60.  
  61.  
  62.         @Override
  63.  
  64.         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  65.  
  66.             Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
  67.  
  68.                     + newVersion + ", which will destroy all old data");
  69.  
  70.             db.execSQL("DROP TABLE IF EXISTS notes");
  71.  
  72.             onCreate(db);
  73.  
  74.         }
  75.  
  76.     }
  77.  
  78.  
  79.  
  80.     public DbAdapter(Context ctx) {
  81.  
  82.         this.mCtx = ctx;
  83.  
  84.     }
  85.  
  86.  
  87.  
  88.     public DbAdapter open() throws SQLException {
  89.  
  90.         mDbHelper = new DatabaseHelper(mCtx);
  91.  
  92.         db = mDbHelper.getWritableDatabase();
  93.  
  94.         return this;
  95.  
  96.     }
  97.  
  98.    
  99.  
  100.     public void close() {
  101.  
  102.         mDbHelper.close();
  103.  
  104.     }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.    public long createNote(String Model, String Company) {
  111.  
  112.         ContentValues initialValues = new ContentValues();
  113.  
  114.         initialValues.put(KEY_MODEL, Model);
  115.  
  116.         initialValues.put(KEY_COMPANY, Company);
  117.  
  118.        
  119.  
  120.  
  121.  
  122.         return db.insert(DATABASE_TABLE, null, initialValues);
  123.  
  124.     }
  125.  
  126.  
  127.  
  128.   public boolean deleteNote(long rowId) {
  129.  
  130.  
  131.  
  132.         return db.delete(DATABASE_TABLE, KEY_ID + "=" + rowId, null) > 0;
  133.  
  134.     }
  135.  
  136.  
  137.  
  138.    public Cursor fetchAllNotes() {
  139.  
  140.         return db.query(DATABASE_TABLE, new String[] {KEY_MODEL}, null, null, null, null, null);
  141.  
  142.     }
  143.  
  144.  
  145.  
  146.   ....
  147.  
  148.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


The notes_list.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.      android:layout_width="wrap_content"
  7.  
  8.         android:layout_height="wrap_content">
  9.  
  10.    
  11.  
  12.     <ListView android:id="@+id/android:list"
  13.  
  14.          android:layout_width="wrap_content"
  15.  
  16.                 android:layout_height="wrap_content"/>
  17.  
  18.         <TextView android:id="@+id/android:empty"
  19.  
  20.          android:layout_width="wrap_content"
  21.  
  22.                 android:layout_height="wrap_content"
  23.  
  24.                 android:text="No Notes!"/>
  25.  
  26.                
  27.  
  28.                 <TextView
  29.  
  30.         android:id="@+id/text123"
  31.  
  32.         xmlns:android="http://schemas.android.com/apk/res/android"
  33.  
  34.    android:layout_width="fill_parent"
  35.  
  36.    android:layout_height="40px"/>
  37.  
  38.  
  39.  
  40. </LinearLayout>
  41.  
  42.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4



The notes_row.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <TextView
  5.  
  6.         android:id="@+id/text122"
  7.  
  8.         xmlns:android="http://schemas.android.com/apk/res/android"
  9.  
  10.    android:layout_width="fill_parent"
  11.  
  12.    android:layout_height="40px"/>
  13.  
  14.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Please help me.

Thanks,
Leslie.
leslie
Freshman
Freshman
 
Posts: 2
Joined: Sun Mar 22, 2009 7:44 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests