Android CallLog 内容提供商提出 java.lang.IllegalArgumentException

Android CallLog Content Provider raises java.lang.IllegalArgumentException

我正在尝试使用以下代码从 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.

// 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.access0(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)

请注意,您同时声明了:

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

第一行说你没有选择参数,但第二行实际上传递了 "",这算作一个参数。

我认为应该将 mSelectionArgs 设置为 null{}。否则,查询方法将尝试在空子句上插入 "",这可能就是导致问题的原因。

mSelectionArgs 在 [0] 处为空。在使用 getContentResolver 之前,您需要在 [0] 处指定其值。

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