2016-04-26 10 views
0

私は、着信と発信のメソッドを持つアンドロイドアプリを持っています。データベースにNullPointerExceptionが挿入されました

私はPhonecallReceiverで、次の方法で実行する場合:

db.insert("CALLINCOME", null, messageValues); 

を私は(下記参照)の例外を取得します。私は作成が成功したことを確認しました。だから、なぜインサートでクラッシュするのですか? 注:以下に成功

Caused by: java.lang.NullPointerException 
at com.example.phamngochieu.recievercall.PhonecallReceiver.onIncomingCallStarted(PhonecallReceiver.java:59) 
at com.example.phamngochieu.recievercall.PhonecallReceiver.onCallStateChanged(PhonecallReceiver.java:90) 
at com.example.phamngochieu.recievercall.PhonecallReceiver.onReceive(PhonecallReceiver.java:48) 
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2392) 

作成したデータベースは、コードです:

public class PhonecallReceiver extends BroadcastReceiver { 
//The receiver will be recreated whenever android feels like it. We need a static variable to remember data between instantiations 
private static int lastState = TelephonyManager.CALL_STATE_IDLE; 
private static Date callStartTime; 
private static boolean isIncoming; 
private static String savedNumber; //because the passed incoming is only valid in ringing 
public static SQLiteDatabase db; 

@Override 
public void onReceive(Context context, Intent intent) { 

    //We listen to two intents. The new outgoing call only tells us of an outgoing call. We use it to get the number. 
    if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) { 
     savedNumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER"); 
    } 
    else{ 
     String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE); 
     String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER); 
     int state = 0; 
     if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){ 
      state = TelephonyManager.CALL_STATE_IDLE; 
     } 
     else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){ 
      state = TelephonyManager.CALL_STATE_OFFHOOK; 
     } 
     else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){ 
      state = TelephonyManager.CALL_STATE_RINGING; 
     } 


     onCallStateChanged(context, state, number); 
    } 
} 

//Derived classes should override these to respond to specific events of interest 
protected void onIncomingCallStarted(Context ctx, String number, Date start) 
{ 
    ContentValues messageValues = new ContentValues(); 
    messageValues.put("PHONENUMBER",number); 
    messageValues.put("TIME",start.toString()); 
    Log.e(number, start.toString()); 
    db.insert("CALLINCOME", null, messageValues); 
    Log.e("gọi đến","*******"); 

} 
protected void onOutgoingCallStarted(Context ctx, String number, Date start) 
{ 
    ContentValues messageValues = new ContentValues(); 
    messageValues.put("PHONENUMBER",number); 
    messageValues.put("TIME", start.toString()); 
    Log.e(number, start.toString()); 
    db.insert("CALLOUTCOME", null, messageValues); 
    Log.e("gọi đi", "*******"); 
} 
protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end){} 
protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end){} 
protected void onMissedCall(Context ctx, String number, Date start){} 

//Deals with actual events 

//Incoming call- goes from IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when its hung up 
//Outgoing call- goes from IDLE to OFFHOOK when it dials out, to IDLE when hung up 
public void onCallStateChanged(Context context, int state, String number) { 
    if(lastState == state){ 
     //No change, debounce extras 
     return; 
    } 
    switch (state) { 
     case TelephonyManager.CALL_STATE_RINGING: 
      isIncoming = true; 
      callStartTime = new Date(); 
      savedNumber = number; 
      onIncomingCallStarted(context, number, callStartTime); 
      break; 
     case TelephonyManager.CALL_STATE_OFFHOOK: 
      //Transition of ringing->offhook are pickups of incoming calls. Nothing done on them 
      if(lastState != TelephonyManager.CALL_STATE_RINGING){ 
       isIncoming = false; 
       callStartTime = new Date(); 
       onOutgoingCallStarted(context, savedNumber, callStartTime); 
      } 
      break; 
     case TelephonyManager.CALL_STATE_IDLE: 
      //Went to idle- this is the end of a call. What type depends on previous state(s) 
      if(lastState == TelephonyManager.CALL_STATE_RINGING){ 
       //Ring but no pickup- a miss 
       onMissedCall(context, savedNumber, callStartTime); 
      } 
      else if(isIncoming){ 
       onIncomingCallEnded(context, savedNumber, callStartTime, new Date()); 
      } 
      else{ 
       onOutgoingCallEnded(context, savedNumber, callStartTime, new Date()); 
      } 
      break; 
    } 
    lastState = state; 
} 

}

とクラスDatabaseHelperが

public class DatabaseHelper extends SQLiteOpenHelper { 
// tên của CSDL 
private static final String DB_NAME = "message"; 
// Version của database 
private static final int DB_VERSON =2; 
public static SQLiteDatabase db; 
public DatabaseHelper(Context context) { 
    super(context, DB_NAME, null, DB_VERSON); 
} 
public static long count; 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // bảng này dùng để lưu tin nhắn đến và đi 
    this.db = db; 

    // bảng này dùng để lưu cuộc gọi đến 
    db.execSQL("CREATE TABLE CALLINCOME (" 
      + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " 
      + "PHONENUMBER TEXT, " 
      + "TIME TEXT);"); 
    // bảng này lưu thông tin cuộc gọi đi 
    db.execSQL("CREATE TABLE CALLOUTCOME (" 
      + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " 
      + "PHONENUMBER TEXT, " 
      + "TIME TEXT);"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

} 

を怒鳴るとManifestfileが

を怒鳴ります
<receiver 
android:name=".PhonecallReceiver" 
android:enabled="true" 
android:exported="true"> 
<intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED"/> 
    <action android:name="android.intent.action.QUICKBOOT_POWERON" /> 
    <action android:name="android.intent.action.PHONE_STATE" /> 
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> 
</intent-filter> 

+0

そここんにちは、あなたは何を意味することができ、あなたのタイトルを言い換える必要があります! –

+0

あなたのログを投稿できますか? – vanloc

+0

私を助けることができますか? –

答えて

2

あなたdbので、まずあなたがinsert()を経由してデータベースの内容を変更できるようにすることへの新しいDatabaseHelperインスタンスと呼びgetWriteableDatabase()を指すように必要なすべてのコード内のどこにも割り当てられませんでした。

変更:

db.insert("CALLINCOME", null, messageValues); 

へ:

db = new DatabaseHelper(getContext()).getWriteableDatabase(); 
db.insert("CALLINCOME", null, messageValues); 
+0

ありがとう –

関連する問題