我想通过以下代码从特定号码获取未读消息。
public void getUnreadMessage() {
Cursor smsInboxCursor1 = getContentResolver().query(
Uri.parse("content://sms/inbox"), new String[] {},
"read = 0 and address='" + pre_address + "'", null, null);
int indexBody = smsInboxCursor1.getColumnIndex("body");
int indexAddress = smsInboxCursor1.getColumnIndex("address");
if (indexBody < 0 || !smsInboxCursor1.moveToFirst())
return;
do {
String str = "SMS From: " + smsInboxCursor1.getString(indexAddress)
+ "\n" + smsInboxCursor1.getString(indexBody) + " \n";
fromNumber = smsInboxCursor1.getString(indexAddress);
smsBody.add(smsInboxCursor1.getString(indexBody));
// arrayAdapter.add(str);
status.add(false);
} while (smsInboxCursor1.moveToNext());
}
但是当我尝试使用以下代码调用此函数时,我得到null异常。
try {
getUnreadMessage();
} catch (Exception e) {
Toast.makeText(this, "Unread message is "+e.getMessage(),Toast.LENGTH_LONG).show();
}
异常是“null”异常。问题出在哪儿 ?我怎么解决这个问题? 完整的堆栈跟踪如下:
09-17 12:23:16.482: E/AndroidRuntime(6882): Caused by: java.lang.NullPointerException
09-17 12:23:16.482: E/AndroidRuntime(6882): at com.commlink.smscheck.SmsActivity.getUnreadMessage(SmsActivity.java:102)
09-17 12:23:16.482: E/AndroidRuntime(6882): at com.commlink.smscheck.SmsActivity.onCreate(SmsActivity.java:63)
09-17 12:23:16.482: E/AndroidRuntime(6882): at android.app.Activity.performCreate(Activity.java:5264)
09-17 12:23:16.482: E/AndroidRuntime(6882): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
09-17 12:23:16.482: E/AndroidRuntime(6882): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
09-17 12:23:16.482: E/AndroidRuntime(6882): ... 11 more