Search code examples
javaandroidandroid-contentprovider

Android CallLog Content Provider raises java.lang.IllegalArgumentException


I'm trying to access information from the Android CallLog Content Provider, using the code that follows, but it raises an exception java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range. The statement has 0 parameters.

// 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)

Solution

  • Notice that you're declaring both:

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

    The first line says that you have no selection arguments but the second line is actually passing "", which counts as one argument.

    I think that should set mSelectionArgs to null or to {}. Otherwise, the query method will try to insert "" on an empty clause and that might be what's causing the problem.