2010-09-27 65 views
1

我正在開發一個應用程序,通過內容解析器獲取傳出的SMS內容,但它給出了一些問題。請幫幫我。光標值null

package com.company.cellSafetyZone.model; 
import android.app.Service; 
import android.content.ComponentName; 
import android.content.Context; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Handler; 
import android.os.IBinder; 
import android.util.Log; 
public class OutGoingSmsService extends Service 
{ Context context; 
    SmsObserver smsObserver; 
    Cursor cursor; 
    Handler handler=new Handler(); 
    private String TAG="SMS SERVICE"; 
    boolean change; 
    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    public OutGoingSmsService() 
    { 
     Log.d(TAG,"blank constructor"); 
     // TODO Auto-generated constructor stub 
    } 
    public OutGoingSmsService(Context context) 
    { 
     Log.d(TAG,"context constructor"); 
     this.context=context; 

    } 
    @Override 
    public void onCreate() 
    { Log.d(TAG,"in on Create() of sms service"); 
     super.onCreate(); 
     smsObserver=new SmsObserver(handler); 
     Log.d(TAG,"calling service"); 
     startService(); 
    } 
    private void startService() 
    { Log.d(TAG,"registering resolver"); 
    cursor=this.getContentResolver().query(Uri.parse("content://sms/sent"), null,null,null,null); 
     Log.d(TAG,"registering observer"); 
     cursor.registerContentObserver(smsObserver); 
     Log.d(TAG,"registerd"); 
    } 
    void setChangeValue(boolean selfChange) 
    {System.out.println("in setChange"); 
     change=selfChange; 
     if(change==false) 
     {  try 
{  cursor=context.getContentResolver().query(Uri.parse("content://sms/sent"), null,null,null,null); 
      cursor.moveToFirst(); 
      }catch (Exception e) 
      { System.out.println("in catch."+e.getMessage()); 
       e.printStackTrace(); 
      } 
      String protocol=cursor.getString(cursor.getColumnIndex("protocol")); 
      Log.d(TAG,"protocol value."+protocol); 
      if(protocol==null) 
       onSmsSend(); 
      }} 
    private void onSmsSend() 
    { cursor.moveToFirst(); 
     for (int i = 0; i <cursor.getColumnCount(); i++) 
     {String column=cursor.getColumnName(i); 
      String msg=cursor.getString(i); }}} 

它呼籲觀察員和onSmsSend()方法再次回到這一服務。但現在給出一個例外。

java.lang.NullPointerException 
atcom.company.cellSafetyZone.model.OutGoingSmsService.setChangeValue(OutGoingSmsService.java:104) 
at com.company.cellSafetyZone.model.SmsObserver.onChange(SmsObserver.java:42) 
at android.database.ContentObserver$NotificationRunnable.run(ContentObserver.java:43) 
at android.os.Handler.handleCallback(Handler.java:587) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:123) 
at android.app.ActivityThread.main(ActivityThread.java:4203) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:521) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549) 
at dalvik.system.NativeStart.main(Native Method) 

爲什麼光標爲空?有一件我無法趕上的小東西請幫助我。

+0

請完善帖子格式! – WarrenFaith 2010-09-27 08:56:54

+0

你瞄準的是哪個android版本? – st0le 2010-11-30 12:29:05

回答

0

在註冊之前打印一些關於光標內部的日誌語句。像:

Log.d(TAG, cursor.getString(c.getColumnIndex("body")));

我問你這樣做,因爲不是所有的手機存儲的短信數據與內容提供商。無論出於何種原因,沒有官方的方式來獲取短信數據,只是很多應用和製造商決定解決這個問題。

+0

這個問題得到解決...... thnkas :)) – nimi 2011-03-25 12:36:04

+0

好,我很高興。如果我的答案解決了問題,請用對號將它標記爲正確:) – eternalmatt 2011-03-26 02:24:37

0

我沒有看到任何錯誤的編程,因爲你能夠在其他部分獲得遊標,但是當你獲得空指針異常時,我認爲你應該在遊標查詢後添加每個間隔的日誌並且寫遊標的列數,這樣你就可以找到任何錯誤的代碼。


我想你可以在你的onsmsend函數中再次添加遊標查詢。