数据库插入时出现 NullPointerException [重复]
Posted
技术标签:
【中文标题】数据库插入时出现 NullPointerException [重复]【英文标题】:NullPointerException on database insert [duplicate] 【发布时间】:2016-04-26 06:20:08 【问题描述】:我有一个具有传入和传出呼叫方法的 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)
和下面的清单文件
<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】:您的db
没有在代码中的任何位置分配,因此首先您需要将一个新的DatabaseHelper
实例指向它并调用getWriteableDatabase()
以便能够通过insert()
更改数据库内容.
变化:
db.insert("CALLINCOME", null, messageValues);
收件人:
db = new DatabaseHelper(getContext()).getWriteableDatabase();
db.insert("CALLINCOME", null, messageValues);
【讨论】:
以上是关于数据库插入时出现 NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章
将记录插入 phpmyadmin 时出现 Asynctask 错误
使用 UCanAccess 插入 ResultSet 时出现“必须在插入之前设置所有列”错误