6

I'm writing an MMS/SMS application on a rooted Android 2.3.3 htc incredible S with sense 2.1. Now, while trying to receive a certain contacts list of SMS/MMS's I get a FC in the query.

here's some code:

Uri uri = Uri.parse("content://mms-sms/conversations/"+String.valueOf(thread_id));
ContentResolver cr = context.getContentResolver();
Cursor c= cr.query(uri,null,null,null,null);    //crashing here

logcat:

05-22 15:24:24.273: E/AndroidRuntime(32087): FATAL EXCEPTION: main
05-22 15:24:24.273: E/AndroidRuntime(32087): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uiu.messaging/com.uiu.messaging.ContactMessageChain}: java.lang.NullPointerException
05-22 15:24:24.273: E/AndroidRuntime(32087): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.app.ActivityThread.access$1500(ActivityThread.java:132)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.os.Looper.loop(Looper.java:143)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.app.ActivityThread.main(ActivityThread.java:4196)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at java.lang.reflect.Method.invoke(Method.java:507)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at dalvik.system.NativeStart.main(Native Method)
05-22 15:24:24.273: E/AndroidRuntime(32087): Caused by: java.lang.NullPointerException
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.os.Parcel.readException(Parcel.java:1328)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:372)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:408)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.content.ContentResolver.query(ContentResolver.java:264)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at com.uiu.messaging.Data.getContactChain(Data.java:319)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at com.uiu.messaging.ContactMessageChain.onCreate(ContactMessageChain.java:138)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
05-22 15:24:24.273: E/AndroidRuntime(32087):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
05-22 15:24:24.273: E/AndroidRuntime(32087):    ... 11 more

All permissions are accounted for.

Any help will be greatly appreciated.

EDIT:

After following Mike's suggestion I receive the following error in the logcat:

05-22 16:31:46.178: E/AndroidRuntime(1892): FATAL EXCEPTION: main
05-22 16:31:46.178: E/AndroidRuntime(1892): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uiu.messaging/com.uiu.messaging.ContactMessageChain}: android.database.sqlite.SQLiteException: near "*": syntax error: , while compiling: SELECT * FROM (SELECT DISTINCT date * 1 AS normalized_date, NULL AS * FROM sms WHERE (htc_category= 0 AND thread_id = 25 AND (type != 3 AND type > 0)) UNION SELECT DISTINCT date * 1000 AS normalized_date, NULL AS * FROM pdu LEFT JOIN pending_msgs ON pdu._id = pending_msgs.msg_id WHERE (htc_category= 0 AND thread_id = 25 AND msg_box != 3 AND (msg_box > 0 AND msg_box != 3 AND (m_type = 128 OR m_type = 132 OR m_type = 130))) ORDER BY normalized_date ASC)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.os.Looper.loop(Looper.java:143)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.app.ActivityThread.main(ActivityThread.java:4196)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at java.lang.reflect.Method.invokeNative(Native Method)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at java.lang.reflect.Method.invoke(Method.java:507)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at dalvik.system.NativeStart.main(Native Method)
05-22 16:31:46.178: E/AndroidRuntime(1892): Caused by: android.database.sqlite.SQLiteException: near "*": syntax error: , while compiling: SELECT * FROM (SELECT DISTINCT date * 1 AS normalized_date, NULL AS * FROM sms WHERE (htc_category= 0 AND thread_id = 25 AND (type != 3 AND type > 0)) UNION SELECT DISTINCT date * 1000 AS normalized_date, NULL AS * FROM pdu LEFT JOIN pending_msgs ON pdu._id = pending_msgs.msg_id WHERE (htc_category= 0 AND thread_id = 25 AND msg_box != 3 AND (msg_box > 0 AND msg_box != 3 AND (m_type = 128 OR m_type = 132 OR m_type = 130))) ORDER BY normalized_date ASC)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:372)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.content.ContentProviderProxy.query(ContentProviderNative.java:408)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.content.ContentResolver.query(ContentResolver.java:264)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at com.uiu.messaging.Data.getContactChain(Data.java:318)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at com.uiu.messaging.ContactMessageChain.onCreate(ContactMessageChain.java:138)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
05-22 16:31:46.178: E/AndroidRuntime(1892):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
05-22 16:31:46.178: E/AndroidRuntime(1892):     ... 11 more

I have no idea what's wrong with the SQL statement...

zwebie
  • 2,349
  • 1
  • 27
  • 43
  • 1
    Attaching the stack trace from logcat is usually wise. – Jens May 22 '12 at 13:04
  • What is the value of the uri before calling query? – MikeIsrael May 22 '12 at 13:50
  • 1
    as written above the thread_id is a long – zwebie May 22 '12 at 14:01
  • This seems to crash on a Galaxy Note 2 with Android 4.3. The error is `Caused by: android.database.sqlite.SQLiteException: no such column: normalized_date (code 1): , while compiling: SELECT * FROM threads ORDER BY normalized_date desc` . How can you determine if the normalized_date column does not exist? I have tested this on other versions of android without an issue. – Etienne Lawlor Apr 05 '14 at 21:22

2 Answers2

4

OK, fixed it. the trick was to enter actual column names into the projection. Seems like the bug was android's after all.

zwebie
  • 2,349
  • 1
  • 27
  • 43
1

According to this other answer the second parameter shouldn't be null(although the documentation says that is a valid value), try

final String[] projection = new String[]{"*"};
Cursor query = contentResolver.query(uri, projection, null, null, null);

original post

Community
  • 1
  • 1
MikeIsrael
  • 2,871
  • 2
  • 22
  • 34
  • This is incorrect. The [**documentation**](http://tinyurl.com/cl7ygch) states that the `projection` argument is "The list of columns to put into the cursor. If `null` all columns are included." – Alex Lockwood May 22 '12 at 14:04
  • @AlexLockwood Yeah I saw that in the documentation but since he was getting an error, thought about the possibility of a bug in Android. I believe if he adds a projection besides "*" it would return an empty set without an error, further suggesting a possible android bug. – MikeIsrael May 22 '12 at 14:19
  • I've used `ContentProvider`s before and can assure you it's not a bug in the documentation... passing `null` is perfectly acceptable. – Alex Lockwood May 22 '12 at 14:20
  • I'm not sure why that question you posted says you need to pass `*`... this sort of SQL syntax is mostly abstracted by the Android SDK. – Alex Lockwood May 22 '12 at 14:21
  • @AlexLockwood as I referenced in the answer, it is based on another post on SO where they claim that the second param can't be null, even though the documentation claims it can be. – MikeIsrael May 22 '12 at 14:30
  • Well it seemed to have helped by user's answer so let's agree to disagree. – MikeIsrael May 23 '12 at 06:58
  • Hmm... that seems very odd. I have never had this problem... weird! – Alex Lockwood May 23 '12 at 07:01