尝试在Android上阅读SMS/MMS并获取java.lang.NullPointerException [英] Trying to read SMS/MMS on Android and geting java.lang.NullPointerException

查看:154
本文介绍了尝试在Android上阅读SMS/MMS并获取java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Android上阅读SMS/MMS,并且在编写代码时遵循了 answer 并尝试在三星设备上的Android OS 6.0.1上运行它,但出现以下异常:

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)

这是创建异常的代码:

        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);

最后一行是导致崩溃的那一行. 即使我使用过:

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

和:

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

或:

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

坠毁发生.

当我尝试在ContactsContract.PhoneLookup.CONTENT_FILTER_URI上运行查询时,查询成功.

是什么原因导致崩溃?

解决方案

在许多其他三星设备上也会发生这种情况: 似乎在这些设备上,没有?simple=true后缀就无法查询content://mms-sms/conversations?simple=true-添加后缀会影响返回的列,这就是投影失败的原因.

有关此问题的更多相关信息,请参见此处,尽管没有人真正知道它为什么如此运行:((

使用未记录的URI content://mms-sms/complete-conversations可能有一种解决方法-您可以在此处阅读. >

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?

解决方案

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.

这篇关于尝试在Android上阅读SMS/MMS并获取java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆