我正在尝试在 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)
这是创建异常的代码:
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
,查询成功。
可能是什么问题导致崩溃?