0

我创建了一个 CursorLoader 来与我的Contentprovider一起工作(单击以查看提供程序),但我收到了一个swapCursor错误

“未为类型 CursorAdapter 定义方法 swapCursor(Cursor)”

将 SwapCursor 更改为 changeCursor 以消除错误。

新错误是加载程序或提供程序试图提取 sqlite 数据。请看logcat

任何帮助将不胜感激。

日志猫:

03-10 10:35:02.788: E/AndroidRuntime(1318): FATAL EXCEPTION: ModernAsyncTask #1
03-10 10:35:02.788: E/AndroidRuntime(1318): java.lang.RuntimeException: An error occured while executing doInBackground()
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:137)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at java.lang.Thread.run(Thread.java:856)
03-10 10:35:02.788: E/AndroidRuntime(1318): Caused by: android.database.sqlite.SQLiteException: no such table: _id: , while compiling: SELECT sName, wUrl, uName, pWord, lNotes FROM _id


03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:354)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:291)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at com.loginplus.home.ListProvider.query(ListProvider.java:61)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.content.ContentProvider$Transport.query(ContentProvider.java:178)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.content.ContentResolver.query(ContentResolver.java:310)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:49)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.support.v4.content.CursorLoader.loadInBackground(CursorLoader.java:35)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:240)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123)
03-10 10:35:02.788: E/AndroidRuntime(1318):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-10 10:35:02.788: E/AndroidRuntime(1318):     ... 4 more

装载机活动:

public class LoginList extends FragmentActivity implements OnClickListener, OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {

private ListView loginList;
private Button webLogin;
private CursorAdapter adapter ;
private static final int LOADER_ID = 02;

private ArrayList<LoginDetails> loginArrayList;

@Override 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.login_listview);
getSupportLoaderManager().initLoader(LOADER_ID, null, this);

String[] from = { dataStore.COLUMN_NAME_SITE, dataStore.COLUMN_NAME_ADDRESS, dataStore.COLUMN_NAME_USERNAME, dataStore.COLUMN_NAME_PASSWORD, dataStore.COLUMN_NAME_NOTES};
int[] to = {R.id.rusName, R.id.ruwUrl, R.id.ruuName, R.id.rupWord, R.id.ruNotes};

adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to);


loginList = (ListView)
findViewById(R.id.loginlist);
loginList.setOnItemClickListener(this);

webLogin = (Button)
findViewById(R.id.button3);
webLogin.setOnClickListener(this);  
}

public Loader<Cursor> onCreateLoader(int id, Bundle args) {

String[] projection = { dataStore.COLUMN_NAME_SITE, dataStore.COLUMN_NAME_ADDRESS, dataStore.COLUMN_NAME_USERNAME, dataStore.COLUMN_NAME_PASSWORD, dataStore.COLUMN_NAME_NOTES};
CursorLoader cursorloader = new CursorLoader(this, ListProvider.CONTENT_URI, projection, null , null, null);
return  cursorloader;   
 }
 public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
adapter.changeCursor(cursor); 
}
 public void onLoaderReset(Loader<Cursor> cursorLoader) {
adapter.changeCursor(null);  
}


@Override
public void onClick (View v) {
Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
startActivity(webLoginIntent);
}

public List<String> populateList (){

List<String> webNameList = new ArrayList<String>();

dataStore openHelperClass = new dataStore (this);

SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();

Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);

while (cursor.moveToNext()){
String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));

LoginDetails lpDetails = new LoginDetails();
 lpDetails.setsName(sName);
 lpDetails.setwUrl(wUrl);
 lpDetails.setuName(uName);
 lpDetails.setpWord(pWord);
 lpDetails.setlNotes(lNotes);

 loginArrayList.add(lpDetails);
 webNameList.add(sName);
}

 cursor.close();
 return webNameList;
}

SQLite 数据库:

 String sqlDataStore = "create table if not exists " +
        TABLE_NAME_INFOTABLE + " ("+ BaseColumns._ID + " integer primary key autoincrement,"

                    + COLUMN_NAME_SITE + " text not null,"
                    + COLUMN_NAME_ADDRESS + " text not null,"
                    + COLUMN_NAME_USERNAME + " text not null,"
                    + COLUMN_NAME_PASSWORD + " text not null,"
                    + COLUMN_NAME_NOTES + " text not null);";

        db.execSQL(sqlDataStore);
    }
4

1 回答 1

1

为了在适配器中可用,您的 SQLite 表必须有一个“_id”列。看起来你的桌子没有。通常的做法是将其声明为整数主键自动增量。检查这个答案

于 2013-03-11T19:11:31.363 回答