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

百般思念 提交于 2019-12-25 09:22:30

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/41154259/trying-to-read-sms-mms-on-android-and-geting-java-lang-nullpointerexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!