Trying to read SMS/MMS on Android and geting java.lang.NullPointerException

1.8k Views Asked by At

I'm trying to read SMS/MMS on Android, and I have followed the answer, when writing the code and try to run it on an Android OS 6.0.1 on Samsung device I got the following exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                      at android.os.Parcel.readException(Parcel.java:1626)
                      at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                      at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                      at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
                      at android.content.ContentResolver.query(ContentResolver.java:502)
                      at android.content.ContentResolver.query(ContentResolver.java:445)
                      at com.my.code.services.ListenSmsMmsService$SMSObserver.onChange(ListenSmsMmsService.java:102)

This is the code that is creating the exception:

        public void onChange(boolean selfChange) {
            super.onChange(selfChange);


            /*first of all we need to decide message is Text or MMS type.*/
            final String[] projection = new String[] {"*"};

            Uri mainUri = Telephony.MmsSms.CONTENT_CONVERSATIONS_URI; //URI for query


            Cursor mainCursor = contentResolver.query(mainUri, projection, null, null, null);

The last line is the one that causes the crash. even if I used:

Uri mainUri = Uri.parse("content://mms-sms/conversations/");

and:

final String[] projection = new String[]{"_id", "ct_t"};

or:

final String[] projection = new String[]{Telephony.MmsSms.TYPE_DISCRIMINATOR_COLUMN};

the crash happen.

When I tried to run a query on ContactsContract.PhoneLookup.CONTENT_FILTER_URI the query was successfull.

What can be the problem, that causes the crash?

2

There are 2 best solutions below

0
Yoav Feuerstein On

This happens on many other Samsung devices: seems that on these devices, you can't query for content://mms-sms/conversations?simple=true without the ?simple=true suffix - and when you add this suffix, it affects the returned columns, and that's why the projection failed.

See here for more related info about this, though no one really knows why it behaves this way :(

There might be a workaround, using an undocumented URI of content://mms-sms/complete-conversations - as you can read here.

0
davidwong37 On

I had the same problem with Samsung devices, so for just SMS conversations, I tried doing grouping by thread id on the SMS table here.

Unfortunately content://mms-sms/complete-conversations doesn't seem to aggregate on the thread id like content://mms-sms/conversations does, so you end up with multiple messages from the same thread.