2012-02-26 3 views
1

나는 android app을 만들려고 노력하고 있으며, 같은 자리에 Force Close을 유지하는 것처럼 보입니다. (setListAdapter/SimpleCursorAdapter) 지금 내가하는 일은 중요합니다. DBAdapter, ListActivity 및 레이아웃 (list/list row)과 이클립스 LogCat 파일도 있습니다. 나는 그것을 실행하지 않는 원인이나 이유를 늘 무엇 확실하지 않다 :안드로이드 setListAdapter/SimpleCursorAdapter

DBAdapter: 
package com.m4ltech.PasswordGenerator; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 
public class PG_DBAdapter { 
private static final String dbName = "PsdGen.db"; 
private static final int Database_Version = 2; 
private static final String CatTbl = "CatTbl"; 
private static final String tempPswdTable = "tmpPswdTbl"; 
private static final String PswdGenTable = "PswdGenTable"; 
public static final String colID = "_CatID"; 
public static final String catName = "CatName"; 
    public static final String tempID = "_tempId"; 
public static final String tmpPswd = "tmpPswd"; 
public static final String PswdGen_ID = "_pswdgenID"; 
public static final String Comp_Site = "company_site"; 
public static final String UserName = "uname"; 
public static final String Category = "category"; 
public static final String Pswd = "password"; 
private static final String TAG = "PG_DBAdapter"; 
private DatabaseHelper mDbHelper; 
private SQLiteDatabase mDb; 
private final Context mCtx; 
private static class DatabaseHelper extends SQLiteOpenHelper { 
    DatabaseHelper(Context context) { super(context, dbName, null, Database_Version); 
    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE " + CatTbl + "(" + colID 
     + " INTEGER PRIMARY KEY AUTOINCREMENT, " + catName 
     + " text not null)"); 
     db.execSQL("CREATE TABLE " + tempPswdTable + "(" + tempID 
     + " INTEGER PRIMARY KEY AUTOINCREMENT, " + tmpPswd + " text not 
          null)"); 
     db.execSQL("CREATE TABLE " + PswdGenTable + "(" + PswdGen_ID 
      + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Comp_Site 
      + " text not null, " + UserName + " text not null, " 
            + Category + " text not null, " + Pswd + " text not null)"); 
     InsertCategories(db); 
     InsertPswdStarter(db); 
    } 
    @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 " + PswdGenTable); 
     db.execSQL("DROP TABLE IF EXISTS " + tempPswdTable); 
     db.execSQL("DROP TABLE IF EXISTS " + CatTbl); 
     onCreate(db); }  } 
public PG_DBAdapter(Context ctx) { this.mCtx = ctx; 
} 
public PG_DBAdapter open() throws SQLException {mDbHelper = new DatabaseHelper(mCtx); 
    mDb = mDbHelper.getWritableDatabase(); return this; 
} 
public void close() { mDbHelper.close();  } 
public long createSavedPswd(String company_site, String uname, 
    String category, String password) { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(Comp_Site, company_site); 
     initialValues.put(UserName, uname); 
     initialValues.put(Category, category); 
     initialValues.put(Pswd, password); 
     return mDb.insert(PswdGenTable, null, initialValues); 
} 
public boolean deletePassword(long rowId) { return mDb.delete(PswdGenTable, PswdGen_ID 
       + "=" + rowId, null) > 0;    } 
public Cursor fetchAllPasswords() { 
    return mDb.query(PswdGenTable, new String[] { PswdGen_ID, Comp_Site, UserName, 
      Category, Pswd }, null, null, null, null, null, null); 
} 
public Cursor fetchPassword(long rowId) throws SQLException { 
    Cursor mCursor = mDb.query(true, PswdGenTable, new String[] { 
      PswdGen_ID, Comp_Site, UserName, Category, Pswd }, PswdGen_ID 
      + " = " + rowId, null, null, null, null, null); 
    if (mCursor != null) { mCursor.moveToFirst(); } 
    return mCursor; 
} 
public boolean updatePassword(long rowId, String uname, String category, 
      String comp_site, String password) { ContentValues args = new ContentValues(); 
      arrgs.put(Comp_Site, comp_site); args.put(UserName, uname); args.put(Category, 
      category); args.put(Pswd, password); return mDb.update(PswdGenTable, args, 
      PswdGen_ID + " = " + rowId, null) > 0; 
} 
static void InsertCategories(SQLiteDatabase db) { 
    ContentValues cv = new ContentValues(); 
    cv.put(colID, 1); 
    cv.put(catName, "Bussiness Web"); 
    db.insert(CatTbl, null, cv); 
    cv.put(colID, 2); 
    cv.put(catName, "Bussiness"); 
    db.insert(CatTbl, null, cv); 
    cv.put(colID, 3); 
    cv.put(catName, "Personal Web"); 
    db.insert(CatTbl, null, cv); 
    cv.put(colID, 4); 
    cv.put(catName, "Personal"); 
    db.insert(CatTbl, null, cv); 
} 
static void InsertPswdStarter(SQLiteDatabase db) { 
    ContentValues cv = new ContentValues(); 
    cv.put(PswdGen_ID, 1); 
    cv.put(Comp_Site, "AOL"); 
    cv.put(UserName, "bsullivan"); 
    cv.put(Category, "Bussiness Web"); 
    cv.put(Pswd, "Ksj6LmKJ"); 
    db.insert(PswdGenTable, null, cv); 
    cv.put(PswdGen_ID, 2); 
    cv.put(Comp_Site, "MSN"); 
    cv.put(UserName, "sullivanb"); 
    cv.put(Category, "Personal Web"); 
    cv.put(Pswd, "KJKsj6Lm"); 
    db.insert(PswdGenTable, null, cv); 
    } 
    } 

ListActivity 

package com.m4ltech.PasswordGenerator; 
import com.m4ltech.PasswordGenerator.R; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.view.ContextMenu; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ContextMenu.ContextMenuInfo; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.AdapterView.AdapterContextMenuInfo; 
import android.widget.Toast; 
public class PGListActivity extends ListActivity { 
private static final int ACTIVITY_CREATE = 0; 
private static final int ACTIVITY_EDIT = 1; 
private PG_DBAdapter mDbHelper; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.list); 
    mDbHelper = new PG_DBAdapter(this); 
    mDbHelper.open(); 
    fillData(); 
    registerForContextMenu(getListView()); 
} 
private void fillData() { 
    Cursor passwordsCursor = mDbHelper.fetchAllPasswords(); 
    startManagingCursor(passwordsCursor); 
    String[] from = new String[] { PG_DBAdapter.Comp_Site}; 
    int[] to = new int[] { R.id.comp_site_row}; 
    SimpleCursorAdapter passwords = new SimpleCursorAdapter(this, R.layout.list_row, 
       passwordsCursor, from, to); setListAdapter(passwords); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    super.onCreateOptionsMenu(menu); 
    MenuInflater mi = getMenuInflater(); 
    mi.inflate(R.menu.main_menu, menu); 
    return true; 
} 
@Override 
public boolean onMenuItemSelected(int featureId, MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.menu_insert: 
     createPassword(); 
     return true; 
    case R.id.menu_settings: 
     Intent l = new Intent(this, PGPreferences.class); 
     startActivity(l); 
     return true; 
    } 
    return super.onMenuItemSelected(featureId, item); 
     } 
@Override 
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    MenuInflater mi = getMenuInflater(); 
    mi.inflate(R.menu.mainmenuitemlngpres, menu); 
} 
@Override 
public boolean onContextItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.menu_delete: 
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
     mDbHelper.deletePassword(info.id); 
     fillData(); 
     return true;  } 
    return super.onContextItemSelected(item); } 
@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    Intent i = new Intent(this, PGEditActivity.class); 
    i.putExtra(PG_DBAdapter.PswdGen_ID, id); 
    startActivityForResult(i, ACTIVITY_EDIT);  } 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 
    super.onActivityResult(requestCode, resultCode, intent); 
    fillData(); } 
private void createPassword() { 
    Intent i = new Intent(this, PGEditActivity.class); 
    startActivityForResult(i, ACTIVITY_CREATE); } 
    } 

list_row.xml 

<?xml version="1.0" encoding="utf-8"?> 
<TextView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/comp_site_row" android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:padding="10dip" /> 

list.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="fill_parent" 
android:layout_gravity="center"  android:background="@color/background" 
android:orientation="vertical"  android:paddingTop="4dp" > 

<ListView 
    android:id="@+id/android:list" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
<TextView 
    android:id="@+id/android:empty" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:text="@string/no_pswd_gen" 
    android:textColor="#000000" />  
</LinearLayout> 

로그 캣 : 예를 들어

02-25 22:59:00.515: E/AndroidRuntime(274): FATAL EXCEPTION: main 
02-25 22:59:00.515: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.m4ltech.PasswordGenerator/com.m4ltech.PasswordGenerator.PGListActivity}: 
java.lang.IllegalArgumentException: column '_id' does not exist 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity 
(ActivityThread.java:2663) 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity 
(ActivityThread.java:2679) 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.ActivityThread.access$2300 
(ActivityThread.java:125) 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage 
(ActivityThread.java:2033) 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123) 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.ActivityThread.main 
ActivityThread.java:4627) 
02-25 22:59:00.515: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native 
Method) 
02-25 22:59:00.515: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521) 
02-25 22:59:00.515: E/AndroidRuntime(274): at 
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
02-25 22:59:00.515: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main 
(ZygoteInit.java:626) 
02-25 22:59:00.515: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method) 
02-25 22:59:00.515: E/AndroidRuntime(274): Caused by: java.lang.IllegalArgumentException: 
column '_id' does not exist 
02-25 22:59:00.515: E/AndroidRuntime(274): at 
android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314) 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.widget.CursorAdapter.init 
(CursorAdapter.java:111) 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.widget.CursorAdapter.<init> 
(CursorAdapter.java:90) 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.widget.ResourceCursorAdapter.<init> 
(ResourceCursorAdapter.java:47) 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.widget.SimpleCursorAdapter.<init> 
(SimpleCursorAdapter.java:84) 
02-25 22:59:00.515: E/AndroidRuntime(274): at 
com.m4ltech.PasswordGenerator.PGListActivity.fillData(PGListActivity.java:51) 
02-25 22:59:00.515: E/AndroidRuntime(274): at 
com.m4ltech.PasswordGenerator.PGListActivity.onCreate(PGListActivity.java:39) 
02-25 22:59:00.515: E/AndroidRuntime(274): at android.app.Instrumentation.callActivityOnCreate 
(Instrumentation.java:1047) 
02-25 22:59:00.515: E/AndroidRuntime(274): at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 

답변

1

커서 어댑터 (SimpleCursorAdapter,), 제공된 커서에 _id 열이 있어야합니다.

테이블에 _id 열이 없으면 추가하거나 SELECT 문에 해당 열을 포함해야합니다. 미래의 문제에 대한

가 밀접 로그 캣을 확인하십시오

내가 말하고 무슨 일이 있었는지입니다
ComponentInfo{com.m4ltech.PasswordGenerator/com.m4ltech.PasswordGenerator.PGListActivity}: 
java.lang.IllegalArgumentException: column '_id' does not exist 
+0

. 그러나 일요일 아침 일찍, 당신은 훨씬 더 좋다고 말했습니다. –

+0

고마워, 자바/안드로이드가 열 명명법에 대해 까다로웠다는 것을 몰랐다 ... PswdGen_ID를 _id로 변경했는데 효과적이다. 다시 한번 감사드립니다. – Les