2011-07-23 7 views
0

私はAndroidデベロッパーズガイドからメモ帳チュートリアルの修正を実装しようとしていますが、起動時に私のアプリケーションが予期せず停止します。起動時に呼び出されAndroid Notepadチュートリアル修正 - 起動時にアプリが予期せず停止する - レイアウト/表示の問題?

私の主な方法...

public class MyList extends ListActivity 
{ 
    private MyListDBAdapter listDBHelper; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate (Bundle savedInstanceState) 
    { 
    super.onCreate (savedInstanceState); 
    setContentView (R.layout.home_layout); 

    listDBHelper = new MyListDBAdapter (this); 
    listDBHelper.open(); 
    fillData(); 

    Button btnAddItem = (Button) findViewById (R.id.add_item_button); 
    btnAddItem.setOnClickListener (new OnClickListener() 
    { 
     @Override 
     public void onClick (View v) 
     { 
     Intent goToAddItem = new Intent (MyList.this,AddItem.class); 
     startActivity(goToAddItem); 
     } 
    }); 
    } 

    private void fillData() 
    { 
    // Get all of the items from the database and create the item list 
    Cursor allItems = listDBHelper.fetchAllItems(); 
    startManagingCursor (allItems); 

    String [] from = new String [] {MyListDBAdapter.FIELD_ATTR1, 
            MyListDBAdapter.FIELD_ATTR2, 
            MyListDBAdapter.FIELD_ATTR3}; 
    int [] to = new int [] {R.id.list_attr1, R.id.list_attr2, 
        R.id.list_attr3}; 

    // Now create an array adapter and set it to display using the item_record_view layout 
    SimpleCursorAdapter items = new SimpleCursorAdapter(this,R.layout.item_record_view, 
                 allItems, from, to); 
    setListAdapter(items); 
    } 
} 

そのレイアウトXMLファイル...

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
android:id = "@+id/home_layout" 
android:layout_width = "wrap_content" 
android:layout_height = "wrap_content" 
xmlns:android = "http://schemas.android.com/apk/res/android"> 
<Button android:id = "@+id/add_item_button" 
     android:layout_width = "fill_parent" 
     android:layout_height = "wrap_content" 
     android:text = "@string/add_item"/> 
<ListView android:id = "@android:id/list" 
      android:layout_width = "wrap_content" 
      android:layout_height = "wrap_content"/> 
<TextView android:id = "@android:id/empty" 
      android:layout_width = "wrap_content" 
      android:layout_height = "wrap_content" 
      android:text = "@string/no_items"/> 
</LinearLayout> 

...と私が使用しようとしているリストビュー:ここに私のコードです私の項目を表示するには...

<?xml version="1.0" encoding="utf-8"?> 
<ListView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView android:id = "@+id/list_attr1" 
      android:layout_width = "wrap_content" 
      android:layout_height = "wrap_content" 
      android:layout_alignParentLeft = "true" 
      xmlns:android="http://schemas.android.com/apk/res/android"/> 
<TextView android:id = "@+id/list_attr2" 
      android:layout_width = "wrap_content" 
      android:layout_height = "wrap_content" 
      android:paddingLeft = "60dip" 
      xmlns:android="http://schemas.android.com/apk/res/android"/> 
<TextView android:id = "@+id/list_attr3" 
      android:layout_width = "wrap_content" 
      android:layout_height = "wrap_content" 
      android:paddingLeft = "160dip" 
      android:gravity = "left" 
      xmlns:android="http://schemas.android.com/apk/res/android"/> 
</ListView> 

MyListDBAdapterのコード...

public class MyListDBAdapter 
{ 
    private static final String TABLE_ITEMS = "MyList"; 
    public static final String FIELD_RECORD_ID = "_ID"; 
    public static final String FIELD_TYPE = "Type"; 
    public static final String FIELD_ATTR1 = "Attr1"; 
    public static final String FIELD_ATTR2 = "Attr2"; 
    public static final String FIELD_ATTR3 = "Attr3"; 
    public static final String FIELD_MY_RATING = "MyRating"; 
    public static final String [] COLUMNS = {FIELD_RECORD_ID, FIELD_TYPE, FIELD_ATTR1, 
              FIELD_ATTR2, FIELD_ATTR3}; 

    private static final String TAG = "MyListDBHelper"; 
    private DatabaseHelper listDBHelper; 
    private SQLiteDatabase itemDB; 

    /** 
    * Database creation sql statement 
    */ 
    private static final String DATABASE_CREATE = ("CREATE TABLE IF NOT EXISTS " + 
               TABLE_ITEMS + " (" + FIELD_RECORD_ID + 
               " INTEGER PRIMARY KEY AUTOINCREMENT," 
               + FIELD_TYPE + " TEXT NOT NULL, " + 
               FIELD_ATTR1 + " INTEGER NOT NULL, " + 
               FIELD_ATTR2 + " TEXT" + " NOT NULL," 
               + FIELD_ATTR3 + " TEXT NOT NULL," + 
               FIELD_MY_RATING + " INTEGER);"); 

    private static final String DATABASE_NAME = "Items"; 
    private static final int DATABASE_VERSION = 1; 

    private final Context thisContext; 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
    DatabaseHelper (Context context) 
    { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate (SQLiteDatabase db) 
    { 
     db.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
     Log.w (TAG, "Upgrading database from version " + oldVersion + " to " 
      + newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS notes"); 
     onCreate(db); 
    } 
    } 

    /** 
    * Constructor - takes the context to allow the database to be 
    * opened/created 
    * 
    * @param context the Context within which to work 
    */ 
    public MyListDBAdapter (Context context) 
    { 
     this.thisContext = context; 
    } 

    /** 
    * Open the items database. If it cannot be opened, try to create a new 
    * instance of the database. If it cannot be created, throw an exception to 
    * signal the failure 
    * 
    * @return this (self reference, allowing this to be chained in an 
    *   initialization call) 
    * @throws SQLException if the database could be neither opened or created 
    */ 
    public MyListDBAdapter open() throws SQLException 
    { 
     listDBHelper = new DatabaseHelper (thisContext); 
     itemsDB = listDBHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close() 
    { 
     listDBHelper.close(); 
    } 

    /** 
     * Create a new item entry using the information provided. If the item is 
     * successfully created return the new record ID for that item, otherwise return 
     * a -1 to indicate failure. 
     * 
     * @param Type the type of the item 
     * @param attr1 
     * @param attr2 
     * @param attr3 
     * @return rowId or -1 if failed 
     */ 
     public long addItem (String type, int attr1, String attr2, String attr3) 
     { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put (FIELD_TYPE, type); 
     initialValues.put (FIELD_ATTR1, attr1); 
     initialValues.put (FIELD_ATTR2, attr2); 
     initialValues.put (FIELD_ATTR3, attr3); 

     return itemsDB.insert (TABLE_ITEMS, null, initialValues); 
     } 

     public long addItem (String type, int attr1, String attr2, String attr3, 
          int myRating) 
     { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put (FIELD_TYPE, type); 
     initialValues.put (FIELD_ATTR1, attr1); 
     initialValues.put (FIELD_ATTR2, attr2); 
     initialValues.put (FIELD_ATTR3, attr3); 
     initialValues.put (FIELD_MY_RATING, myRating); 

     return itemsDB.insert (TABLE_ITEMS, null, initialValues); 
     } 

     /** 
     * Delete the item with the given record ID 
     * 
     * @param _ID ID of item to delete 
     * @return true if deleted, false otherwise 
     */ 
     public boolean deleteItem (long recordID) 
     { 
     return (itemsDB.delete (TABLE_ITEMS, FIELD_RECORD_ID + "=" + recordID, 
       null) > 0); 
     } 

     /** 
     * Return a Cursor over the list of all items in the database 
     * 
     * @return Cursor over all notes 
     */ 
     public Cursor fetchAllItems() 
     { 
      return itemsDB.query (TABLE_ITEMS, COLUMNS, null, null, null, null, null); 
     } 

     /** 
     * Return a Cursor positioned at the item that matches the given record ID 
     * 
     * @param _ID ID of item to retrieve 
     * @return Cursor positioned to matching item, if found 
     * @throws SQLException if item could not be found/retrieved 
     */ 
     public Cursor fetchItem (long recordID) throws SQLException 
     { 
      Cursor itemCursor = itemsDB.query (true, TABLE_ITEMS, COLUMNS, 
         FIELD_RECORD_ID + "=" + recordID, null, null, null, null, null); 
      if (itemCursor != null) 
      { 
      itemCursor.moveToFirst(); 
      } 

      return itemCursor; 
     } 

     /** 
      * Update the item using the details provided. The item to be updated is 
      * specified using the record ID, and it is altered to use the values passed in 
      * 
      * @param _ID ID of item to update 
      * @param type type value to set item type to 
      * @param attr1 
      * @param attr2 
      * @param attr3 
      * @return true if the item was successfully updated, false otherwise 
      */ 
      public boolean updateItem (long recordID, String type, int attr1, 
            String attr2, String attr3) 
      { 
      ContentValues editVals = new ContentValues(); 
      editVals.put (FIELD_TYPE, type); 
      editVals.put (FIELD_ATTR1, attr1); 
      editVals.put (FIELD_ATTR2, attr2); 
      editVals.put (FIELD_ATTR3, attr3); 

      return (itemsDB.update (TABLE_ITEMS, editVals, FIELD_RECORD_ID + "=" + 
        recordID, null) > 0); 
      } 
} 

問題は私のAddItemクラスまたはそれに関連付けられたレイアウトではないと私は思っていますが、私はこの記事のコードを書式設定するのは本当にうんざりです。誰が私が何がうまくいかないのを助けることができますか?

+0

あなたはスタックトレースでlogcatのログを投稿した場合、助けがはるかに容易になります。 – MByD

+0

私はlogcatについて知りませんでしたが、間違いなく有用です...言及してくれてありがとう! –

答えて

1

あなたはカーソルが「_id」という名前の列が含まれている必要があり

_id MyListDBAdapterFIELD_RECORD_IDの名前を変更する必要があるか、このクラスでは動作しません。

ドキュメントhere

+0

はい!これは問題でした...ありがとうございました。私のコードで変数を変更したので、最初はちょっとイライラしましたが、アプリケーションはまだ私のAVDの古いデータファイルから読み込んでいました。一度私はそれをすべてクリアしたそれは魅力のように働いた! –

関連する問題