2012-01-11 45 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

這可能發生在蒙面號碼的情況下。