使用ContentResolver更新方法获取InvocationTargetException

时间:2017-05-21 01:04:18

标签: android sqlite

我有一个使用两个互斥的桌子,家务和奖励的应用程序。

杂务更新方法正常。当我使用getContentResolver在奖励表(只有一条记录)上尝试更新方法时,我收到一个InvocationTargetException错误。调试带我去......

try {
    mResolvedMethod.invoke(mResolvedContext, v);
}
catch (IllegalAccessException e) {
    throw new IllegalStateException(
        "Could not execute non-public method for android:onClick", e);
}
catch (InvocationTargetException e) {
    throw new IllegalStateException(
        "Could not execute method for android:onClick", e);
}

这是堆栈消息......似乎与URI有关。

Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
    at android.view.View.performClick(View.java:5246)
    at android.widget.TextView.performClick(TextView.java:10565)
    at android.view.View$PerformClick.run(View.java:21200)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6947)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Caused by: java.lang.UnsupportedOperationException: Unknown uri: content://com.example.adrianhps.choresapp4.contentprovider/rewards/1
   at com.example.adrianhps.choresapp4.ChoresProvider.update(ChoresProvider.java:200)
   at android.content.ContentProvider$Transport.update(ContentProvider.java:341)
   at android.content.ContentResolver.update(ContentResolver.java:1386)
   at com.example.adrianhps.choresapp4.Rewards.onClickAdd(Rewards.java:106)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
   at android.view.View.performClick(View.java:5246)
   at android.widget.TextView.performClick(TextView.java:10565)
   at android.view.View$PerformClick.run(View.java:21200)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:145)
   at android.app.ActivityThread.main(ActivityThread.java:6947)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)

这是我的代码。

public class ChoresContract {

    public static final String CONTENT_AUTHORITY = "com.example.adrianhps.choresapp4.contentprovider";

    private static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);

    public static final String PATH_CHORES = "chores";
    public static final String PATH_REWARDS = "rewards";


    public static final class RewardsEntry implements BaseColumns{
        public static final Uri CONTENT_URI =
                BASE_CONTENT_URI.buildUpon().appendPath(PATH_REWARDS).build();

        public static final String CONTENT_TYPE =
                "vnd.android.cursor.dir/" + CONTENT_URI + "/" + PATH_REWARDS;
        public static final String CONTENT_ITEM_TYPE =
                "vnd.android.cursor.item/" + CONTENT_URI + "/" + PATH_REWARDS;

        public static final String SQLITE_TABLE_REWARDS = "rewards";
        public static final String _ID = "_id";
        public static final String REWARD_COL="reward";
        public static final String CHORESDONE_COL="choresdone";
        public static final String CHORESTARGET_COL="chorestarget";

        public static Uri buildRewardUri(long id){
            return ContentUris.withAppendedId(CONTENT_URI, id);
        }
    }
}

这是DBHelper类的部分:

private void addRewardsTable(SQLiteDatabase db){
    db.execSQL(
        "CREATE TABLE " + ChoresContract.RewardsEntry.SQLITE_TABLE_REWARDS + " (" +
            ChoresContract.RewardsEntry._ID + " INTEGER PRIMARY KEY, " +
            ChoresContract.RewardsEntry.REWARD_COL + " TEXT UNIQUE NOT NULL," +
            ChoresContract.RewardsEntry.CHORESDONE_COL + " TEXT UNIQUE NOT NULL," +
            ChoresContract.RewardsEntry.CHORESTARGET_COL + " TEXT UNIQUE NOT NULL);"
    );
}

以下是更新方法......

@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

    final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int rows;

    switch(sUriMatcher.match(uri)){
        case REWARDS:
            rows = db.update(ChoresContract.RewardsEntry.SQLITE_TABLE_REWARDS, values, selection, selectionArgs);
            break;
        case CHORES:
            rows = db.update(ChoresContract.ChoresEntry.SQLITE_TABLE, values, selection, selectionArgs);
            break;
        default:
            throw new UnsupportedOperationException("Unknown uri: " + uri);
    }

    if(rows != 0){
        getContext().getContentResolver().notifyChange(uri, null);
    }

    return rows;
}

活动代码

public void onClickAdd(View view) {
    String newReward = etRewardToUpdate.getText().toString().trim();
    String newTarget = etNewTarget.getText().toString().trim();
    String choresDone = "0";
    String idUpdate = "1";
    ContentValues values = new ContentValues();
    values.put(ChoresContract.RewardsEntry.REWARD_COL, newReward);
    values.put(ChoresContract.RewardsEntry.CHORESTARGET_COL, newTarget);
    values.put(ChoresContract.RewardsEntry.CHORESDONE_COL, choresDone);

    Uri uri = Uri.parse(ChoresContract.RewardsEntry.CONTENT_URI + "/" + idUpdate);
    getContentResolver().update(uri, values, null, null);
}

0 个答案:

没有答案