访问DB时,广播接收器中的空指针异常

时间:2014-04-10 10:14:34

标签: android

我想从广播接收器中的数据库中访问电话号码以与消息地址进行比较我在SMSlockDB和SMsReciver类中得到空指针异常我指出在我的代码中也因为数据库没有打开我也检查我的方法其他活动它正常工作的电话号码也保存在数据库中 这是我的代码

public class SmsReciever extends BroadcastReceiver {

SQLiteDatabase database;
//Context c;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    // ---get the SMS message passed in---
     SmslockDB no = new SmslockDB(context);

    database = context.openOrCreateDatabase(SmslockDB.DATABASE_NAME, 0, null);

        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String adrrs = "";
        String msbdy = "";
        String messages = "";
        if (bundle != null) {
            // ---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");

            msgs = new SmsMessage[pdus.length];
            for (int i = 0; i < msgs.length; i++) {
                msgs[i] = SmsMessage.createFromPdu((byte[]) 
 pdus[i]);
 adrrs += msgs[i].getOriginatingAddress().toString().trim();
        msbdy += msgs[i].getMessageBody().toString().trim();
        String rmspno = adrrs.replaceAll("\\s+", "");
        String cmpno = no.Spclgetno(rmspno);<------------ here my loggate  
                                                                          show error

                if (rmspno.contains(cmpno)) {

                    messages += "SMS from " + n + " :\n";
                    messages += msbdy + "\n";

                    try {

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    // ---display the new SMS message--
        Toast.makeText(context, messages, Toast.LENGTH_SHORT)
                            .show();
                    this.abortBroadcast();

                }
            }

        }
    }
       }

这是我的数据库

public class SmslockDB {

//  tabel columns
public static final String ROWID_PRF = " _id";
public static final String PRF_NO = "_noto";
public static final String PRF_NAME = "_name";

//  DB Name
public static final String DATABASE_NAME = "SMSlock_DB";
//  DB version
private static final int DATABASE_VERSION = 1 ;
    // table 1 name 
private static final String TABLE_PROFILE = "T_SProle";   // second table name

private DbHelper ourhelper;
private Context ourcontext;
private SQLiteDatabase ourdatabase;
public static class DbHelper extends SQLiteOpenHelper{

    public Context context;
    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
        this.context = context ;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + TABLE_MSSG +"(" +
                ROWID_SM + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                SMS_NO + " TEXT NOT NULL, " +
                SMS_Body + " TEXT NOT NULL );"
                );
        db.execSQL("CREATE TABLE " + TABLE_PROFILE +"(" +
                ROWID_PRF + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                PRF_NO + " TEX NOT NULL, " +
                PRF_NAME + " TEXT NOT NULL );"
                );  
       }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MSSG);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROFILE);
        onCreate(db);

    }
    }
 public SmslockDB(Context c) {
// TODO Auto-generated constructor stub
    ourcontext = c;
}
 public SmslockDB open() throws SQLException{

ourhelper = new DbHelper(ourcontext);
ourdatabase = ourhelper.getWritableDatabase();
return this;
 }
 public void close(){
ourhelper.close();
}
// method that i want to receive in BroadcastReciever 
public String Spclgetno(String spno)  throws SQLException{
// TODO Auto-generated method stub

String[] columns = new String[] {ROWID_PRF, PRF_NO, PRF_NAME };
//ourdatabase = ourhelper.getWritableDatabase();
Cursor c = ourdatabase.query(TABLE_PROFILE, columns,  PRF_NO + "='" + spno + "'",  
    null, null, null, null);<------- also in this line query show exception

if(c != null && c.moveToFirst() ) {     //&& c.getCount() > 0



    String retsno = c.getString(1);
    Log.i("prfno", retsno.toString());
    return retsno;
}
return null;
  }

这是我的loggate

 04-10 15:06:14.923: W/dalvikvm(29010): threadid=1: thread exiting with uncaught   
 exception (group=0x41f852a0)
 04-10 15:06:14.943: E/AndroidRuntime(29010): FATAL EXCEPTION: main
 04-10 15:06:14.943: E/AndroidRuntime(29010): java.lang.RuntimeException: Unable to   
 start receiver com.smslocker.SmsReciever: java.lang.NullPointerException
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
 android.app.ActivityThread.handleReceiver(ActivityThread.java:2269)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
 android.app.ActivityThread.access$1600(ActivityThread.java:134)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at    
 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at    
 android.os.Handler.dispatchMessage(Handler.java:99)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at     
 android.os.Looper.loop(Looper.java:137)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
 android.app.ActivityThread.main(ActivityThread.java:4867)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
 java.lang.reflect.Method.invokeNative(Native Method)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
 java.lang.reflect.Method.invoke(Method.java:511)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at  
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
 dalvik.system.NativeStart.main(Native Method)
 04-10 15:06:14.943: E/AndroidRuntime(29010): Caused by: java.lang.NullPointerException
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at    
 com.smslocker.SmslockDB.Spclgetno(SmslockDB.java:109)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at   
 com.smslocker.SmsReciever.onReceive(SmsReciever.java:51)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   at    
 android.app.ActivityThread.handleReceiver(ActivityThread.java:2262)
 04-10 15:06:14.943: E/AndroidRuntime(29010):   ... 10 more
 04-10 15:06:15.003: W/CursorWrapperInner(29010): Cursor finalized without prior close()
 04-10 15:06:15.003: D/dalvikvm(29010): GC_CONCURRENT freed 311K, 13% free   
 10230K/11719K, paused 12ms+5ms, total 111ms

我在这样的例子中表示

   <receiver
        android:name="com.smslocker.SmsReciever"
        android:exported="true"
        android:enabled="true">
        <intent-filter android:priority="999" >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

0 个答案:

没有答案