Android的ContentResolver.query()传递一个字符串[]投影参数时抛出异常 [英] Android ContentResolver.query() throwing exception when passing a String[] projection argument
问题描述
在我目前的Android项目中,我面临着一个奇怪的错误。
In my current Android project I'm facing with a strange error.
每次我发出<一个href=\"https://developer.android.com/reference/android/content/ContentResolver.html#query(android.net.Uri,%20java.lang.String[],%20java.lang.String,%20java.lang.String[],%20java.lang.String)\"相对=nofollow>查询方法我得到的错误说,通过投影的列的String []的说法是不存在的(_id列字段组成)。尽管如此,传递null (因此允许将返回所有列)的正常工作,所以我必须承担这不是列名不匹配的问题。此外,从Cursor对象检索数据时,列名不给予任何问题,因为我可以放心地通过查询指定相同的列名()获取的所有数据。
Everytime I issue the query method I get errors saying that the columns of the passed projection String[] argument don't exist (_id column field comprised). Despite this, passing null (hence allowing all columns to be returned) works fine, so I must assume this is not a problem of column names mismatch. Also, when retrieving data from a Cursor object, the column names are not giving any problems, since I can safely get all of the data by specifying the same column names on query().
我目前查询的MediaStore.Audio *数据库和清单文件中的所有权限和最终的设置都OK。
I'm currently querying on the MediaStore.Audio.* database, and all the permissions within the Manifest file and eventual settings are ok.
这是我的code片断:
This is my code snippet:
Cursor artists[] = new Cursor[2];
String projection[] = {
Audio.Artists.ARTIST,
Audio.Artists.NUMBER_OF_ALBUMS,
Audio.Artists.NUMBER_OF_TRACKS,
};
artists[0] = this.getActivity().getApplicationContext().getContentResolver().query(
Audio.Artists.INTERNAL_CONTENT_URI,
projection,
null,
null,
Audio.Artists.DEFAULT_SORT_ORDER
);
artists[1] = this.getActivity().getApplicationContext().getContentResolver().query(
Audio.Artists.EXTERNAL_CONTENT_URI,
projection,
null,
null,
Audio.Artists.DEFAULT_SORT_ORDER
);
this.adapter = new ArtistListAdapter(this.getActivity(), new MergeCursor(artists));
只是要清楚,这里就是我使用光标对象:
Just to make it clear, here's where I use the Cursor object:
@Override
public void bindView (View v, Context c, Cursor cursor) {
ImageView icon;
TextView name, artistInfo;
//icon = (ImageView) v.findViewById(R.id.artist_icon);
name = (TextView) v.findViewById(R.id.artist_name);
artistInfo = (TextView) v.findViewById(R.id.artist_info);
//icon.setIcon(icon);
name.setText(cursor.getString(cursor.getColumnIndex(Audio.Artists.ARTIST)));
artistInfo.setText(
String.format(
"%s total albums\n%s total songs",
cursor.getString(cursor.getColumnIndex(Audio.Artists.NUMBER_OF_ALBUMS)),
cursor.getString(cursor.getColumnIndex(Audio.Artists.NUMBER_OF_TRACKS))
)
);
}
发出此code,我收到以下错误:
Issuing this code, I receive the following error:
07-11 21:28:31.221: E/AndroidRuntime(22737): FATAL EXCEPTION: main
07-11 21:28:31.221: E/AndroidRuntime(22737): java.lang.IllegalArgumentException: column '_id' does not exist
07-11 21:28:31.221: E/AndroidRuntime(22737): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
07-11 21:28:31.221: E/AndroidRuntime(22737): at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
有什么可以成为这个问题的原因以及如何解决?到现在为止,我一直在传递投影参数为空,但我不想要检索我不会使用的内容。
What can be the cause of this problem and how can I fix it? Until now, I've been passing null on the projection arguments, but I don't want to retrieve content I won't be using.
请询问您是否需要进一步的信息。
Please ask if you need further informations.
推荐答案
添加BaseColumns._ID投影阵列。这是使用的CursorAdapter时所需的列(我从您正在使用SimpleCursorAdapter上下文假设)
add BaseColumns._ID to projection array. this is the required column when using the CursorAdapter (and i'm assuming from the context that you are using SimpleCursorAdapter)
这篇关于Android的ContentResolver.query()传递一个字符串[]投影参数时抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!