简体   繁体   English

尝试在 Android 上读取短信/彩信并获取 java.lang.NullPointerException

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

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:我正在尝试在 Android 上阅读 SMS/MMS,并且我遵循了答案,在编写代码并尝试在三星设备上的 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)

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.当我尝试在ContactsContract.PhoneLookup.CONTENT_FILTER_URI上运行查询时,查询成功。

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.这发生在许多其他三星设备上:似乎在这些设备上,您无法在没有?simple=true后缀的情况下查询content://mms-sms/conversations?simple=true - 当您添加此后缀时,它会影响返回的列,这就是投影失败的原因。

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 .可能有一种解决方法,即使用content://mms-sms/complete-conversations的未记录 URI - 您可以在此处阅读。

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 .我在三星设备上遇到了同样的问题,因此对于 SMS 对话,我尝试在此处按 SMS 表上的线程 id 进行分组。

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.不幸的是content://mms-sms/complete-conversations似乎不像content://mms-sms/conversations那样聚合在线程 id 上,所以你最终会收到来自同一线程的多条消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM