2012-01-11 43 views
1

我正在从内容提供商读取短信的Android应用程序。该应用程序工作正常,并从内容提供商读取短信罚款。但有时(非常罕见)“地址”列会为短信息返回空值。Android:空间字符串问题的'地址'列短信内容提供商

下面是示例代码,我使用的是什么:

String whereClause = "_id > " + String.valueOf(Database.getLastSmsId(this)); 
     Cursor cursor = getContentResolver().query(smsUri, null, whereClause, null, null); 
     if(cursor.moveToFirst()) { 
      do { 
       int id = cursor.getInt(cursor.getColumnIndex("_id")); 
      String protocol = cursor.getString(cursor.getColumnIndex("protocol")); 
      String body = cursor.getString(cursor.getColumnIndex("body")); 

      String address = cursor.getString(cursor.getColumnIndex("address")); // <----- Here is the problem 
      // address returns as null string 

      String date = cursor.getString(cursor.getColumnIndex("date")); 

       Log.d(Constants.TAG, "SMS event received. address="+address); 
      } while(cursor.moveToNext()); 
    } 

我得到摩托罗拉Driod的Android V2.3.5这个问题。请指教。

Ali

回答

2

我发现了这个问题。 如果开始编写并关闭短信应用程序(由于来电或任何其他原因),android操作系统会将短信保存为短信内容提供商的草稿。这会在“地址”列中使用空值在SMS数据库中生成一个新条目。

萨尔曼

1

这是一个正常的行为。发现在SmsMessage documentation

返回字符串 形式此短信的起始地址(发件人)或NULL如果无法

如果因为NPE的您的应用程序中断后,你应该插入一个空检查并将地址设置为空字符串。

建议:应该问一个问题,如果有错误,应该提供堆栈跟踪。

+0

空值在对话期间发生。我们捕获所有消息,并突然为其中一条消息返回null,而其余消息在只与单个人通信时显示正常。 – Sali 2012-01-11 15:06:49

+0

文档中还说可能发生这种情况。没有解决方案,您应该确保您的应用程序也使用空值。 – WarrenFaith 2012-01-11 15:19:18

0

这可能发生在蒙面号码的情况下。