1. 在执行context.getContentResolver.query()方法时出现错误。
07-15 18:46:13.470: E/AndroidRuntime(13624): FATAL EXCEPTION: AsyncTask #1
07-15 18:46:13.470: E/AndroidRuntime(13624): java.lang.RuntimeException: An error occured while executing doInBackground()
07-15 18:46:13.470: E/AndroidRuntime(13624): at android.os.AsyncTask$3.done(AsyncTask.java:278)
07-15 18:46:13.470: E/AndroidRuntime(13624): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-15 18:46:13.470: E/AndroidRuntime(13624): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-15 18:46:13.470: E/AndroidRuntime(13624): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-15 18:46:13.470: E/AndroidRuntime(13624): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-15 18:46:13.470: E/AndroidRuntime(13624): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-15 18:46:13.470: E/AndroidRuntime(13624): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-15 18:46:13.470: E/AndroidRuntime(13624): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-15 18:46:13.470: E/AndroidRuntime(13624): at java.lang.Thread.run(Thread.java:856)
07-15 18:46:13.470: E/AndroidRuntime(13624): Caused by: java.lang.NullPointerException
07-15 18:46:13.470: E/AndroidRuntime(13624): at android.os.Parcel.readException(Parcel.java:1333)
07-15 18:46:13.470: E/AndroidRuntime(13624): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:182)
07-15 18:46:13.470: E/AndroidRuntime(13624): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
07-15 18:46:13.470: E/AndroidRuntime(13624): at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
07-15 18:46:13.470: E/AndroidRuntime(13624): at android.content.ContentResolver.query(ContentResolver.java:311)
07-15 18:46:13.470: E/AndroidRuntime(13624): at com.txrj.sms.manager.SmsDataManager2.getThreads(SmsDataManager2.java:25)
07-15 18:46:13.470: E/AndroidRuntime(13624): at com.txrj.sms.activity.ConversationListActivity$LoadThreadsTask.doInBackground(ConversationListActivity.java:92)
07-15 18:46:13.470: E/AndroidRuntime(13624): at com.txrj.sms.activity.ConversationListActivity$LoadThreadsTask.doInBackground(ConversationListActivity.java:1)
07-15 18:46:13.470: E/AndroidRuntime(13624): at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-15 18:46:13.470: E/AndroidRuntime(13624): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-15 18:46:13.470: E/AndroidRuntime(13624): ... 5 more
下面红色部分代码是产生错误的地方。原因是根本无法从Telephony.Threads.CONTENT_URI中查询数据。
应该使用下面绿色部分的代码来实现查询threads表。
Cursor cursor = context.getContentResolver().query(Telephony.Sms.CONTENT_URI,
new String[] {"* from threads order by date desc--"},
null, null, null);
public static List<TxrjThreads> getThreads(Context context) {
List<TxrjThreads> list = new ArrayList<TxrjThreads>();
Cursor cursor = context.getContentResolver().query(Telephony.Threads.CONTENT_URI,
new String[]{
Telephony.Threads._ID,
Telephony.Threads.DATE,
Telephony.Threads.RECIPIENT_IDS,
Telephony.Threads.MESSAGE_COUNT,
Telephony.Threads.READ,
Telephony.Threads.SNIPPET,
Telephony.Threads.SNIPPET_CHARSET,
Telephony.Threads.TYPE,
Telephony.Threads.ERROR,
Telephony.Threads.HAS_ATTACHMENT
},
null, null, Telephony.Threads.DATE + " desc");
if (cursor != null && cursor.moveToFirst()) {
do {
TxrjThreads thread = new TxrjThreads();
thread.setThreadId(getLong(cursor, Telephony.Threads._ID));
thread.setTime(getLong(cursor, Telephony.Threads.DATE));
thread.setRecipientId(getString(cursor, Telephony.Threads.RECIPIENT_IDS));
thread.setMessageCount(getInt(cursor, Telephony.Threads.MESSAGE_COUNT));
thread.setRead(getInt(cursor, Telephony.Threads.READ));
thread.setSnippet(getString(cursor, Telephony.Threads.SNIPPET));
thread.setSnippetCS(getInt(cursor, Telephony.Threads.SNIPPET_CHARSET));
thread.setType(getInt(cursor, Telephony.Threads.TYPE));
} while(cursor.moveToNext());
cursor.close();
}
return list;
}
public static long getLong(Cursor cursor, String colunName) {
return cursor.getLong(cursor.getColumnIndex(colunName));
}
public static int getInt(Cursor cursor, String colunName) {
return cursor.getInt(cursor.getColumnIndex(colunName));
}
public static String getString(Cursor cursor, String colunName) {
return cursor.getString(cursor.getColumnIndex(colunName));
}
2. 查看android.content.ContentResolver.query()方法源码。
public final Cursor query(Uri uri, String[] projection,
String selection, String[] selectionArgs, String sortOrder) {
IContentProvider provider = acquireProvider(uri);
if (provider == null) {
return null;
}
try {
long startTime = SystemClock.uptimeMillis();
Cursor qCursor = provider.query(uri, projection, selection, selectionArgs, sortOrder);
if (qCursor == null) {
releaseProvider(provider);
return null;
}
// force query execution
qCursor.getCount();
long durationMillis = SystemClock.uptimeMillis() - startTime;
maybeLogQueryToEventLog(durationMillis, uri, projection, selection, sortOrder);
// Wrap the cursor object into CursorWrapperInner object
return new CursorWrapperInner(qCursor, provider);
} catch (RemoteException e) {
releaseProvider(provider);
return null;
} catch(RuntimeException e) {
releaseProvider(provider);
throw e;
}
}
3. 查找IContentProvider.java文件所在位置。
android4.2.2\android\frameworks\base\core\java\android\content