尝试将Content Provider与数据库一起使用时获取IllegalArgumentException

时间:2018-01-30 07:29:22

标签: android

与内容提供商合作时,我得到了  无法绑定索引1处的参数,因为索引超出范围。该陈述有0个参数。
我是新手,所以我无法解决错误

MainActivity

  helper=new g_dbHelper((this));
    SQLiteDatabase sql=helper.getWritableDatabase();

    ContentValues values=new ContentValues();
    values.put("NAME","Harry Potter");
    values.put("PUBLISHER","Bloomberg Publications");
    values.put("PRICE",500);
    Uri uri1=getContentResolver().insert(uri,values);
 //   Log.e("ROWID",String.valueOf(newRowId));
    String[] name={"NAME"};
    displayView = (TextView) findViewById(R.id.textViewa);
    SQLiteDatabase sql1=helper.getReadableDatabase();
   try {try{
       cursor = getContentResolver().query(uri, name, null, null, null);
       if (cursor == null)
           displayView.setText("Invalid Selection");
       else {
           cursor.moveToFirst();

           int id = cursor.getColumnIndex("NAME");


           displayView.setText(cursor.getString(id));
           {cursor.close();}}}
           catch(IllegalArgumentException e)
           {
               Log.e("TAG",e.getMessage());
           }
       }

     catch(SQLiteException e)
    {
        Log.e("TAG","Problem in Reading");
    }

g_dbCP.java

 public boolean onCreate() {
    helper=new g_dbHelper(getContext());
    sql=helper.getReadableDatabase();
    uriMatcher=new UriMatcher(UriMatcher.NO_MATCH);
    uriMatcher.addURI("com.example.asus.g_books.provider","first",1);
    uriMatcher.addURI("com.example.asus.g_books.provider","first/#",2);
    return true;
}


@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable 
String s, @Nullable String[] strings1, @Nullable String s1) {
    int match=uriMatcher.match(uri);
    Cursor cursor=null;
    switch(match)
    {
        case 1:
            cursor=sql.query("first",strings,s,strings,null,null,s1);

            break;
        case 2:
            s=g_dbHelper.id+"=?";
            strings1=new String[] 
{String.valueOf(ContentUris.parseId(uri))};
            cursor=sql.query("first",strings,s,strings,null,null,s1);
            break;
    }
    return cursor;
}

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

@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
    SQLiteDatabase sql1=helper.getWritableDatabase();
    switch(uriMatcher.match(uri))
    {
        case 1:
            long newRowId = sql1.insert("first",null,values);
            return ContentUris.withAppendedId(uri,newRowId);

            default:
            throw new IllegalStateException("operation failed");
    }
}

清单

package="com.example.asus.g_books">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<provider
android:authorities="com.example.asus.g_books.provider"
android:name="com.example.asus.g_books.g_dbCP"/>
</application>

这里我只是在表格中添加一本书的细节。 我们也欢迎任何其他建议和建议。

删除catch块时的错误日志

com.example.asus.g_books E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.example.asus.g_books, PID: 21491
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asus.g_books/com.example.asus.g_books.MainActivity}: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range.  The statement has 0 parameters.
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2659)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724)
                                                                          at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:154)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6123)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
                                                                       Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range.  The statement has 0 parameters.
                                                                          at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212)
                                                                          at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)
                                                                          at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200)
                                                                          at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
                                                                          at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
                                                                          at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1165)
                                                                          at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1036)
                                                                          at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1204)
                                                                          at com.example.asus.g_books.g_dbCP.query(g_dbCP.java:42)
                                                                          at android.content.ContentProvider.query(ContentProvider.java:1020)
                                                                          at android.content.ContentProvider$Transport.query(ContentProvider.java:239)
                                                                          at android.content.ContentResolver.query(ContentResolver.java:530)
                                                                          at android.content.ContentResolver.query(ContentResolver.java:472)
                                                                          at com.example.asus.g_books.MainActivity.onCreate(MainActivity.java:56)
                                                                          at android.app.Activity.performCreate(Activity.java:6672)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2612)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2724) 
                                                                          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:154) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6123) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 

0 个答案:

没有答案