2015-02-13 3 views
1

Я пытаюсь получить доступ к информации из Android CallLog Content Provider, используя код, который следует, но это вызывает исключение java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.Android CallLog Content Provider поднимает java.lang.IllegalArgumentException

// Designates which columns to get back from the content provider 
String[] mProjection = 
{ 
    CallLog.Calls.NUMBER, 
    CallLog.Calls.DATE, 
    CallLog.Calls.DURATION, 
    CallLog.Calls.TYPE 
}; 

// Defines a string to contain the selection clause 
String mSelectionClause = null; 

// Initializes an array to contain selection arguments 
String[] mSelectionArgs = {""}; 

Cursor mCursor = getContentResolver().query 
(
     CallLog.Calls.CONTENT_URI,   // The content URI of the words table 
     mProjection,      // The columns to return for each row 
     mSelectionClause,     // Either null, or the word the user entered 
     mSelectionArgs,     // Either empty, or the string the user entered 
     "DATE DESC"      // The sort order for the returned rows 
); 

java.lang.RuntimeException: Unable to start activity ComponentInfo 
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:2211) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
at android.app.ActivityThread.access$600(ActivityThread.java:141) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5103) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 

ответ

1

Обратите внимание, что вы» вновь заявляя, как:

String mSelectionClause = null; 
String[] mSelectionArgs = {""}; 

Первая строка говорит, что у вас нет аргументов выбора, но вторая линия на самом деле проходит "", который считается одним аргументом.

Я думаю, что следует установить mSelectionArgs в null или до {}. В противном случае метод запроса попытается вставить "" в пустое предложение, и это может быть причиной проблемы.

0

mSelectionArgs имеет значение null в [0]. Вы должны указать его значение в [0], прежде чем использовать getContentResolver.

// If the word is the empty string, gets everything 
if (TextUtils.isEmpty(mSearchString)) { 
    // Setting the selection clause to null will return all words 
    mSelectionClause = null; 
    mSelectionArgs[0] = ""; 

} else { 
    // Constructs a selection clause that matches the word that the user entered. 
    mSelectionClause = UserDictionary.Words.WORD + " = ?"; 

    // Moves the user's input string to the selection arguments. 
    mSelectionArgs[0] = mSearchString; 

} 

http://developer.android.com/guide/topics/providers/content-provider-basics.html

Смежные вопросы