数据库插入时出现NullPointerException

时间:2016-04-26 06:20:08

标签: java android sqlite jdbc sql-insert

我有一个Android应用程序,有一个传入和传出的调用方法。

当我在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>

1 个答案:

答案 0 :(得分:2)

您的db未在代码中的任何位置分配,因此首先您需要为其指定一个新的DatabaseHelper实例,并且调用getWriteableDatabase()以便能够更改数据库内容通过insert()

变化:

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

要:

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