Android Content Provider,奇怪的URI:NumberFormatException:invalid long:

时间:2017-01-05 07:23:13

标签: android uri android-contentprovider

当我想更新一行时,我有一个uri的另一个错误,我在使用时收到以下错误:long id = ContentUris.parseId(uri);

我的错误:

01-05 07:11:18.091 32144-32144/com.example.jean.cartememoire E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jean.cartememoire, PID: 32144
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.NumberFormatException: Invalid long: "Cartes_table"
at java.lang.Long.invalidLong(Long.java:124)
at java.lang.Long.parse(Long.java:363)
at java.lang.Long.parseLong(Long.java:353)
at java.lang.Long.parseLong(Long.java:321)
at android.content.ContentUris.parseId(ContentUris.java:86)
at com.example.jean.cartememoire.CardContentProvider.update(CardContentProvider.java:209)
at android.content.ContentProvider$Transport.update(ContentProvider.java:355)
at android.content.ContentResolver.update(ContentResolver.java:1362)
at com.example.jean.cartememoire.Play.setCard(Play.java:176)
at com.example.jean.cartememoire.Play.repondre(Play.java:143)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5198) 
at android.view.View$PerformClick.run(View.java:21147) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616

在我的ContentProvider下面,我有一个删除和插入功能,但效果不错,但更新不起作用。而且我不理解我的错误,为什么会说Invalid long: "Cartes_table"?我在另一个函数中匹配完全相同的东西并且它起作用了

我的内容提供商:

public class CardContentProvider extends ContentProvider {
    private StockCard stock;

    public static String authority = "com.example.jean.cartememoire.CardContentProvider";

    public static final String _ID = "_id";
    public static final String THEME = "THEME";
    public static final String QUESTION = "QUESTION";
    public static final String REPONSE = "REPONSE";
    public static final String DIFFICULTE = "DIFFICULTE"; //# = un chiffre
    public static final String STOCK_TABLE = "Cartes_table";
    public static final String STOCK_FACILE = "Cartes_table_facile";
    public static final String STOCK_NORMAL = "Cartes_table_normal";
    public static final String STOCK_DIFFICILE = "Cartes_table_difficile";
    public static final String STOCK_EXTREME = "Cartes_table_extreme";
    public static final String STOCK_IMPOSSIBLE = "Cartes_table_impossible";


    private static final int ID_STOCK_TABLE = 1;
    public static final int ID_FACILE = 2;
    public static final int ID_NORMAL = 3;
    public static final int ID_DIFFICILE = 4;
    public static final int ID_EXTREME = 5;
    public static final int ID_IMPOSSIBLE = 6;
    private static final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        matcher.addURI(authority, STOCK_TABLE, ID_STOCK_TABLE);
        matcher.addURI(authority, STOCK_TABLE+"/*", ID_STOCK_TABLE);
        matcher.addURI(authority, STOCK_FACILE, ID_FACILE);
        matcher.addURI(authority, STOCK_NORMAL, ID_NORMAL);
        matcher.addURI(authority, STOCK_DIFFICILE, ID_DIFFICILE);
        matcher.addURI(authority, STOCK_EXTREME, ID_EXTREME);
        matcher.addURI(authority, STOCK_IMPOSSIBLE, ID_IMPOSSIBLE);
        matcher.addURI(authority, STOCK_FACILE+"/*", ID_FACILE);
        matcher.addURI(authority, STOCK_NORMAL+"/*", ID_NORMAL);
        matcher.addURI(authority, STOCK_DIFFICILE+"/*", ID_DIFFICILE);
        matcher.addURI(authority, STOCK_EXTREME+"/*", ID_EXTREME);
        matcher.addURI(authority, STOCK_IMPOSSIBLE+"/*", ID_IMPOSSIBLE);

    }


    @Override
    public boolean onCreate() {
        stock = StockCard.getInstance(getContext());
        return true;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = stock.getReadableDatabase();
        int code = matcher.match(uri);
        Cursor cursor;
        switch (code)
        {
            case ID_STOCK_TABLE:
                Log.d("Uri provider = "+code+" ", uri.toString()+" ");
                cursor = db.query(STOCK_TABLE, projection, selection,
                        selectionArgs, null, null, sortOrder);
                break;
            case ID_FACILE:
                cursor = db.query(STOCK_FACILE, projection, selection,
                        selectionArgs, null, null, sortOrder);
                break;
            case ID_NORMAL:
                cursor = db.query(STOCK_NORMAL, projection, selection,
                        selectionArgs, null, null, sortOrder);
                break;
            case ID_DIFFICILE:
                cursor = db.query(STOCK_DIFFICILE, projection, selection,
                        selectionArgs, null, null, sortOrder);
                break;
            case ID_EXTREME:
                cursor = db.query(STOCK_EXTREME, projection, selection,
                        selectionArgs, null, null, sortOrder);
                break;
            case ID_IMPOSSIBLE:
                cursor = db.query(STOCK_IMPOSSIBLE, projection, selection,
                        selectionArgs, null, null, sortOrder);
                break;
            default:
                //long u = ContentUris.parseId(uri);
                Log.d("Uri provider = "+code+" ", uri.toString()+" ");
                throw new UnsupportedOperationException("Pas encore implémenté");
        }

        return cursor;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = stock.getWritableDatabase();
        int code = matcher.match(uri);
        long id;
        System.out.println(uri.toString());
        Uri.Builder builder = new Uri.Builder();

        switch(code)
        {
            case ID_STOCK_TABLE:
                id = db.insert(STOCK_TABLE, null, values);
                builder.appendPath(STOCK_TABLE);
                break;
            case ID_FACILE:
                id = db.insert(STOCK_FACILE, null, values);
                builder.appendPath(STOCK_FACILE);
                break;
            case ID_NORMAL:
                id = db.insert(STOCK_NORMAL, null, values);
                builder.appendPath(STOCK_NORMAL);
                break;
            case ID_DIFFICILE:
                id = db.insert(STOCK_DIFFICILE, null, values);
                builder.appendPath(STOCK_DIFFICILE);
                break;
            case ID_EXTREME:
                id = db.insert(STOCK_EXTREME, null, values);
                builder.appendPath(STOCK_EXTREME);
                break;
            case ID_IMPOSSIBLE:
                id = db.insert(STOCK_IMPOSSIBLE, null, values);
                builder.appendPath(STOCK_IMPOSSIBLE);
                break;
            default:
                throw new UnsupportedOperationException("Pas encore implémenté");
        }

        builder.authority(authority);
        builder = ContentUris.appendId(builder, id);

        return builder.build();
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = stock.getWritableDatabase();
        int code = matcher.match(uri);
        int i;
        long id = ContentUris.parseId(uri);
        System.out.println("URI DELETE : "+id);
        switch(code)
        {
            case ID_STOCK_TABLE:
                id = ContentUris.parseId(uri);
                i = db.delete(STOCK_TABLE, "_id=" + id, null);
                break;
            case ID_FACILE:
                id = ContentUris.parseId(uri);
                i = db.delete(STOCK_FACILE, "_id=" + id, null);
                break;
            case ID_NORMAL:
                id = ContentUris.parseId(uri);
                i = db.delete(STOCK_NORMAL, "_id=" + id, null);
                break;
            case ID_DIFFICILE:
                id = ContentUris.parseId(uri);
                i = db.delete(STOCK_DIFFICILE, "_id=" + id, null);
                break;
            case ID_EXTREME:
                id = ContentUris.parseId(uri);
                i = db.delete(STOCK_EXTREME, "_id=" + id, null);
                break;
            case ID_IMPOSSIBLE:
                id = ContentUris.parseId(uri);
                i = db.delete(STOCK_IMPOSSIBLE, "_id=" + id, null);
                break;
            default:
                throw new UnsupportedOperationException("Pas encore implémenté");
        }

        return 0;
    }

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

        SQLiteDatabase db = stock.getWritableDatabase();
        int code = matcher.match(uri);
        int i;
        long id = ContentUris.parseId(uri);
        System.out.println("URI C "+id);
        switch(code)
        {
            case ID_STOCK_TABLE:
                id = ContentUris.parseId(uri);
                i = db.update(STOCK_TABLE, values, "_id=" + id, selectionArgs);
                break;
            case ID_FACILE:
                id = ContentUris.parseId(uri);
                i = db.update(STOCK_FACILE, values, "_id=" + id, selectionArgs);
                break;
            case ID_NORMAL:
                id = ContentUris.parseId(uri);
                System.out.println("ID NORMAL URI : "+id);
                i = db.update(STOCK_NORMAL, values, "_id=" + id, selectionArgs);
                break;
            case ID_DIFFICILE:
                id = ContentUris.parseId(uri);
                i = db.update(STOCK_DIFFICILE, values, "_id=" + id, selectionArgs);
                break;
            case ID_EXTREME:
                id = ContentUris.parseId(uri);
                i = db.update(STOCK_EXTREME, values, "_id=" + id, selectionArgs);
                break;
            case ID_IMPOSSIBLE:
                id = ContentUris.parseId(uri);
                i = db.update(STOCK_IMPOSSIBLE, values, "_id=" + id, selectionArgs);
                break;
            default:
                throw new UnsupportedOperationException("Pas encore implémenté");
        }

        return 0;
    }
}

我的函数setCard更新我的数据库:

public void setCard(int new_difficulty)
    {

            //Mise à jour dans cartes_table
            ContentValues cv = new ContentValues();
            cv.put(CardContentProvider.THEME, theme);
            cv.put(CardContentProvider.QUESTION, question);
            cv.put(CardContentProvider.REPONSE, reponse);
            cv.put(CardContentProvider.DIFFICULTE, new_difficulty);


            ContentResolver resolver = this.getContentResolver();
            Uri.Builder builder = new Uri.Builder();


            builder.scheme("content").authority(CardContentProvider.authority).appendPath(CardContentProvider.STOCK_TABLE);
            Uri uri = builder.build();
            System.out.println("SET CARD : "+uri.toString());
            int u = resolver.update(uri, cv, null, null);
            System.out.println("SET CARD AFTER"+u);
            long id = ContentUris.parseId(uri);
            Toast.makeText(this, "Carte créé avec succès !", Toast.LENGTH_SHORT).show();

            //Changement de decks (delete + insert)
            //addToDiffDeck(new_difficulty, cv);

    }

谢谢你们!

0 个答案:

没有答案