ContentResolver.query()—>buildQueryString()
Cursor cursor = context.getContentResolver().query(Sms.CONTENT_URI,
new String[]{"thread_id from sms where type = 3 group by thread_id—"}, // 可以这样使用。
null, null, null);
07-17 10:55:17.084: E/AndroidRuntime(30157): FATAL EXCEPTION: main
07-17 10:55:17.084: E/AndroidRuntime(30157): java.lang.NullPointerException
07-17 10:55:17.084: E/AndroidRuntime(30157): at com.txrj.sms.activity.ConversationListActivity$LoadThreadsExtra.run(ConversationListActivity.java:181)
07-17 10:55:17.084: E/AndroidRuntime(30157): at android.os.Handler.handleCallback(Handler.java:605)
07-17 10:55:17.084: E/AndroidRuntime(30157): at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 10:55:17.084: E/AndroidRuntime(30157): at android.os.Looper.loop(Looper.java:137)
07-17 10:55:17.084: E/AndroidRuntime(30157): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-17 10:55:17.084: E/AndroidRuntime(30157): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 10:55:17.084: E/AndroidRuntime(30157): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 10:55:17.084: E/AndroidRuntime(30157): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-17 10:55:17.084: E/AndroidRuntime(30157): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-17 10:55:17.084: E/AndroidRuntime(30157): at dalvik.system.NativeStart.main(Native Method)
Cursor cursor = context.getContentResolver().query(Sms.CONTENT_URI,
new String[]{"thread_id"},
"type = 3 group by thread_id", null, null); // 不可以这样使用。
07-17 11:16:48.988: E/AndroidRuntime(32627): FATAL EXCEPTION: main
07-17 11:16:48.988: E/AndroidRuntime(32627): android.database.sqlite.SQLiteException: near "group": syntax error: , while compiling: SELECT thread_id FROM sms WHERE (type = 3 group by thread_id) ORDER BY date DESC
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:180)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.content.ContentResolver.query(ContentResolver.java:311)
07-17 11:16:48.988: E/AndroidRuntime(32627): at com.txrj.sms.manager.SmsDataManager.getThreadsWithDraftMsg(SmsDataManager.java:33)
07-17 11:16:48.988: E/AndroidRuntime(32627): at com.txrj.sms.activity.ConversationListActivity$LoadThreadsExtra.run(ConversationListActivity.java:170)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.os.Handler.handleCallback(Handler.java:605)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.os.Looper.loop(Looper.java:137)
07-17 11:16:48.988: E/AndroidRuntime(32627): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-17 11:16:48.988: E/AndroidRuntime(32627): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 11:16:48.988: E/AndroidRuntime(32627): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 11:16:48.988: E/AndroidRuntime(32627): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-17 11:16:48.988: E/AndroidRuntime(32627): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-17 11:16:48.988: E/AndroidRuntime(32627): at dalvik.system.NativeStart.main(Native Method)
Cursor cursor = context.getContentResolver().query(Sms.CONTENT_URI,
new String[]{"thread_id"},
"type = 3", null, "thread_id desc group by thread_id"); // 不可以这样使用。
07-17 11:20:57.040: E/AndroidRuntime(940): FATAL EXCEPTION: main
07-17 11:20:57.040: E/AndroidRuntime(940): android.database.sqlite.SQLiteException: near "group": syntax error: , while compiling: SELECT thread_id FROM sms WHERE (type = 3) ORDER BY thread_id desc group by thread_id
07-17 11:20:57.040: E/AndroidRuntime(940): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:180)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:136)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.content.ContentProviderProxy.query(ContentProviderNative.java:358)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.content.ContentResolver.query(ContentResolver.java:311)
07-17 11:20:57.040: E/AndroidRuntime(940): at com.txrj.sms.manager.SmsDataManager.getThreadsWithDraftMsg(SmsDataManager.java:33)
07-17 11:20:57.040: E/AndroidRuntime(940): at com.txrj.sms.activity.ConversationListActivity$LoadThreadsExtra.run(ConversationListActivity.java:170)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.os.Handler.handleCallback(Handler.java:605)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.os.Handler.dispatchMessage(Handler.java:92)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.os.Looper.loop(Looper.java:137)
07-17 11:20:57.040: E/AndroidRuntime(940): at android.app.ActivityThread.main(ActivityThread.java:4517)
07-17 11:20:57.040: E/AndroidRuntime(940): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 11:20:57.040: E/AndroidRuntime(940): at java.lang.reflect.Method.invoke(Method.java:511)
07-17 11:20:57.040: E/AndroidRuntime(940): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
07-17 11:20:57.040: E/AndroidRuntime(940): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
07-17 11:20:57.040: E/AndroidRuntime(940): at dalvik.system.NativeStart.main(Native Method)
com.android.providers.telephony.SmsProvider.query(Uri, String[], String, String[], String)
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor ret = qb.query(db, projectionIn, selection, selectionArgs,
null, null, orderBy);
-->
android.database.sqlite.SQLiteQueryBuilder.query(SQLiteDatabase, String[], String, String[], String, String, String)
public Cursor query(SQLiteDatabase db, String[] projectionIn,
String selection, String[] selectionArgs, String groupBy,
String having, String sortOrder) {
return query(db, projectionIn, selection, selectionArgs, groupBy, having, sortOrder,
null /* limit */);
}
-->
android.database.sqlite.SQLiteQueryBuilder.query(SQLiteDatabase, String[], String, String[], String, String, String, String)
public Cursor query(SQLiteDatabase db, String[] projectionIn,
String selection, String[] selectionArgs, String groupBy,
String having, String sortOrder, String limit) {
if (mTables == null) {
return null;
}
String sql = buildQuery(
projectionIn, selection, selectionArgs, groupBy, having,
sortOrder, limit);
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Performing query: " + sql);
}
return db.rawQueryWithFactory(
mFactory, sql, selectionArgs,
SQLiteDatabase.findEditTable(mTables));
}
-->
android.database.sqlite.SQLiteQueryBuilder.buildQuery(String[], String, String[], String, String, String, String)
public String buildQuery(
String[] projectionIn, String selection, String[] selectionArgs,
String groupBy, String having, String sortOrder, String limit) {
String[] projection = computeProjection(projectionIn);
StringBuilder where = new StringBuilder();
boolean hasBaseWhereClause = mWhereClause != null && mWhereClause.length() > 0;
if (hasBaseWhereClause) {
where.append(mWhereClause.toString());
where.append(')');
}
// Tack on the user's selection, if present.
if (selection != null && selection.length() > 0) {
if (hasBaseWhereClause) {
where.append(" AND ");
}
where.append('(');
where.append(selection);
where.append(')');
}
return buildQueryString(
mDistinct, mTables, projection, where.toString(),
groupBy, having, sortOrder, limit);
}
-->
android.database.sqlite.SQLiteQueryBuilder.buildQueryString(boolean, String, String[], String, String, String, String, String)
public static String buildQueryString(
boolean distinct, String tables, String[] columns, String where,
String groupBy, String having, String orderBy, String limit) {
if (TextUtils.isEmpty(groupBy) && !TextUtils.isEmpty(having)) {
throw new IllegalArgumentException(
"HAVING clauses are only permitted when using a groupBy clause");
}
if (!TextUtils.isEmpty(limit) && !sLimitPattern.matcher(limit).matches()) {
throw new IllegalArgumentException("invalid LIMIT clauses:" + limit);
}
StringBuilder query = new StringBuilder(120);
query.append("SELECT ");
if (distinct) {
query.append("DISTINCT ");
}
if (columns != null && columns.length != 0) {
appendColumns(query, columns);
} else {
query.append("* ");
}
query.append("FROM ");
query.append(tables);
appendClause(query, " WHERE ", where);
appendClause(query, " GROUP BY ", groupBy);
appendClause(query, " HAVING ", having);
appendClause(query, " ORDER BY ", orderBy);
appendClause(query, " LIMIT ", limit);
return query.toString();
}
-->
private static void appendClause(StringBuilder s, String name, String clause) {
if (!TextUtils.isEmpty(clause)) {
s.append(name);
s.append(clause);
}
}
class LoadThreadsExtra implements Runnable {
List<Integer> draftIds = null;
List<Integer> failIds = null;
Map<Integer, Integer> unreadMap = null;
Map<Integer, String> canonicalAddrMap = null;
Map<String, String> nameAddrMap = null;
@Override
public void run() {
draftIds = SmsDataManager.getThreadsWithDraftMsg(mContext);
failIds = SmsDataManager.getThreadsWithFailedMsg(mContext);
unreadMap = SmsDataManager.getThreadUnreadCountMap(mContext);
canonicalAddrMap = SmsDataManager.getCanonicalAddressMap(mContext);
nameAddrMap = SmsDataManager.getNameAddressMap(mContext);
int threadCount = mThreads.size();
for(int i=0;i<threadCount;i++){
TxrjThreads thread = mThreads.get(i);
long threadId = thread.getThreadId();
thread.setHasDraftMsg(draftIds.contains(threadId)); // draftIds是Integer类型的列表,而threadId是long类型。决定不可能返回true!
thread.setHasFailMsg(failIds.contains(threadId)); // 同上
thread.setUnReadCount(unreadMap.containsKey(threadId) ? unreadMap.get(threadId) : 0); // 同上
Log.i("txrjsms", "threadId:"+threadId+", hasDraftMsg:"+thread.isHasDraftMsg()
+", hasFailMsg:"+thread.isHasFailMsg()+", unread:"+thread.getUnReadCount());
}
mHandler.sendEmptyMessage(TxrjConstant.WHAT_NOTIFY_DATA_CHANGED);
}
}
07-17 11:58:20.379: I/txrjsms(2402): show data in listview.
07-17 11:58:21.470: I/txrjsms(2402): threadId:455, hasDraftMsg:false, hasFailMsg:true, unread:1
07-17 11:58:21.470: I/txrjsms(2402): threadId:459, hasDraftMsg:true, hasFailMsg:true, unread:0
07-17 11:58:21.470: I/txrjsms(2402): threadId:457, hasDraftMsg:false, hasFailMsg:false, unread:0
07-17 11:58:21.470: I/txrjsms(2402): threadId:458, hasDraftMsg:false, hasFailMsg:false, unread:0
07-17 11:58:21.470: I/txrjsms(2402): threadId:456, hasDraftMsg:false, hasFailMsg:false, unread:0
点击某个会话跳转到信息列表界面,没有显示任何信息出来。
查看代码定位到产生bug的原因是getIntent().getIntExtra()无法获取到long类型的threadId。
ConversationListActivity.java
it.putExtra(TxrjConstant.EXTRA_THREAD_ID, thread.getThreadId());
MessageListActivity.java
mThreadId = getIntent().getIntExtra(TxrjConstant.EXTRA_THREAD_ID, -1);
将getIntExtra改成getLongExtra。如下。
mThreadId = getIntent().getLongExtra(TxrjConstant.EXTRA_THREAD_ID, -1);