问题
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