第二に、私は本当にアンドロイドに慣れていないので、それがどのように機能するかを完全に把握していません (悲しいことに、オンラインで見つけることができるチュートリアルのほとんどを書いた人々に当てはまるようです。 )。問題を進めて、関連するコードのみを投稿しようとします。スキップした場合はお知らせください。
マニフェスト エントリ:
<provider android:name="MyContentProvider" android:authorities="hua.it20910.android.provider"/>
主なアクティビティ コール:
Cursor c = getContentProvider().query(MyContentProvider.getTableUri(1), null, null, null, null);
コンテンツ プロバイダ:
private static SQLiteDatabase db;
private static final String database_name = "contactlist.db";
private static final int database_version = 1;
private static final String table1="Contacts";
private static final String[] table1_row={"_id","Surname","Name","Phone","Email","Address","Group"};
private static final String table2="Groups";
private static final String[] table2_row={"_id","Name"};
public static final String PROVIDER_NAME = "hua.it20910.android.provider";
public static final Uri CONTACTS_URI = Uri.parse("content://"+ PROVIDER_NAME + "/"+table1);
public static final Uri GROUPS_URI = Uri.parse("Content://"+PROVIDER_NAME + "/"+table2);
private static final int CONTACTS = 1;
private static final int CONTACT_ID = 2;
private static final int GROUPS = 3;
private static final int GROUP_ID = 4;
private static final UriMatcher myMatcher;
static {
myMatcher = new UriMatcher(UriMatcher.NO_MATCH);
myMatcher.addURI(PROVIDER_NAME, table1, CONTACTS);
myMatcher.addURI(PROVIDER_NAME, table1+"/#", CONTACT_ID);
myMatcher.addURI(PROVIDER_NAME, table2, GROUPS);
myMatcher.addURI(PROVIDER_NAME, table1+"/#", GROUP_ID);
}
private static final String table1Sql = "CREATE TABLE "
+ table1 + "("
+ table1_row[0] + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ table1_row[1] + " VARCHAR(25), "
+ table1_row[2] + " VARCHAR(25), "
+ table1_row[3] + " INTEGER, "
+ table1_row[4] + " VARCHAR(25), "
+ table1_row[5] + " VARCHAR(25)) ";
private static final String table2Sql = "CREATE TABLE "
+ table2 + "("
+ table2_row[0] + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ table2_row[1] + " VARCHAR(25))";
private class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper(Context context) {
super(context, database_name, null, database_version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(table1Sql);
db.execSQL(table2Sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+table1 );
db.execSQL("DROP TABLE IF EXISTS "+table2 );
onCreate(db);
}
}
public static Uri getTableUri(int i){
if (i==1){
return CONTACTS_URI;
}
if (i==2){
return GROUPS_URI;
}
else{
return null;
}
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder();
switch (myMatcher.match(uri)){
case CONTACTS:
sqlBuilder.setTables(table1);
break;
case CONTACT_ID:
sqlBuilder.setTables(table1);
sqlBuilder.appendWhere("_id=" + uri.getPathSegments().get(1));
break;
case GROUPS:
sqlBuilder.setTables(table2);
break;
case GROUP_ID:
sqlBuilder.setTables(table2);
sqlBuilder.appendWhere("_id=" + uri.getPathSegments().get(1));
break;
default:
throw new SQLException("Wrong uri " + uri);
}
if (sortOrder == null || sortOrder == "")
sortOrder = "_id DESC";
Cursor c = db.query(null, projection, selection, selectionArgs, null, null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return null;
}
タイトルで述べたように、現在取得しているエラー メッセージはクエリの実行時に無効なテーブルですが、ここに到達するために他のエラーを回避するのに苦労してきました。解決したのか、単に歩き回ったのかわかりません将来それらを再び見つけるためだけに、エラーを修正していただければ幸いです。
LogCat:
01-11 05:43:08.385: E/Trace(930): error opening trace file: No such file or directory (2)
01-11 05:43:08.525: I/ActivityThread(930): Pub hua.it20910.android.provider: hua.it20910.android.MyContentProvider
01-11 05:43:09.515: D/dalvikvm(930): GC_FOR_ALLOC freed 57K, 7% free 2583K/2760K, paused 191ms, total 193ms
01-11 05:43:09.515: I/dalvikvm-heap(930): Grow heap (frag case) to 3.241MB for 635812-byte allocation
01-11 05:43:09.565: D/dalvikvm(930): GC_FOR_ALLOC freed 1K, 6% free 3202K/3384K, paused 43ms, total 43ms
01-11 05:43:09.616: D/dalvikvm(930): GC_CONCURRENT freed <1K, 6% free 3214K/3384K, paused 5ms+13ms, total 52ms
01-11 05:43:09.635: D/AndroidRuntime(930): Shutting down VM
01-11 05:43:09.645: W/dalvikvm(930): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-11 05:43:09.655: E/AndroidRuntime(930): FATAL EXCEPTION: main
01-11 05:43:09.655: E/AndroidRuntime(930): java.lang.RuntimeException: Unable to start activity ComponentInfo{hua.it20910.android/hua.it20910.android.MainActivity}: java.lang.IllegalStateException: Invalid tables
01-11 05:43:09.655: E/AndroidRuntime(930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.os.Looper.loop(Looper.java:137)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-11 05:43:09.655: E/AndroidRuntime(930): at java.lang.reflect.Method.invokeNative(Native Method)
01-11 05:43:09.655: E/AndroidRuntime(930): at java.lang.reflect.Method.invoke(Method.java:511)
01-11 05:43:09.655: E/AndroidRuntime(930): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-11 05:43:09.655: E/AndroidRuntime(930): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-11 05:43:09.655: E/AndroidRuntime(930): at dalvik.system.NativeStart.main(Native Method)
01-11 05:43:09.655: E/AndroidRuntime(930): Caused by: java.lang.IllegalStateException: Invalid tables
01-11 05:43:09.655: E/AndroidRuntime(930): at android.database.sqlite.SQLiteDatabase.findEditTable(SQLiteDatabase.java:971)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
01-11 05:43:09.655: E/AndroidRuntime(930): at hua.it20910.android.MyContentProvider.query(MyContentProvider.java:178)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.content.ContentProvider.query(ContentProvider.java:652)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.content.ContentProvider$Transport.query(ContentProvider.java:189)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.content.ContentResolver.query(ContentResolver.java:372)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.content.ContentResolver.query(ContentResolver.java:315)
01-11 05:43:09.655: E/AndroidRuntime(930): at hua.it20910.android.MainActivity.displayRecords(MainActivity.java:31)
01-11 05:43:09.655: E/AndroidRuntime(930): at hua.it20910.android.MainActivity.onCreate(MainActivity.java:19)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.app.Activity.performCreate(Activity.java:5104)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-11 05:43:09.655: E/AndroidRuntime(930): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-11 05:43:09.655: E/AndroidRuntime(930): ... 11 more