我正在构建一个测试应用程序,以根据数据库列表阻止对手机的来电。数据库运行正常,我遇到的问题是呼叫接收器。当我尝试读取数据库并根据数据库中的值检查传入调用时,我收到错误。
我正在使用的代码:
package com.call.blocker;
import java.lang.reflect.Method;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import com.android.internal.telephony.ITelephony;
public class CustomBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "Phone Call";
Context context = null;
private ITelephony telephonyService;
public String[] GatherNumbers() {
int i = 0;
Dbhelper dbHelper = new Dbhelper(CustomBroadcastReceiver.this);
SQLiteDatabase db = dbHelper.getReadableDatabase(); //Problem is here
Cursor cursor = db.rawQuery("SELECT " + Dbhelper.C_NUM + " FROM " + Dbhelper.TABLE, null);
String[] myNumbers = new String[cursor.getCount() - 1];
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
myNumbers[i] = cursor.getString(cursor.getColumnIndex(Dbhelper.C_NUM));
i++;
} while (cursor.moveToNext());
}
}
return myNumbers;
}
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (null == bundle)
return;
Log.v(TAG, bundle.toString() + " 1");
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
Log.v(TAG, "State: " + state + " 2");
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {
String phonenumber = bundle
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.v(TAG, "Incomng Number: " + phonenumber);
try {
String[] MyNumbers = GatherNumbers();
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.silenceRinger();
telephonyService.endCall();
Log.v(TAG, "Ending Call");
} catch (Exception e) {
e.printStackTrace();
Log.v(TAG, "Error Ending Call");
}
}
}
}
在LogCat中给我这个:
java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
at com.call.blocker.CustomBroadcastReceiver.GatherNumbers(CustomBroadcastReceiver.java:22)
at com.call.blocker.CustomBroadcastReceiver.onReceive(CustomBroadcastReceiver.java:59)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:1769)
at android.app.ActivityThread.access$2400(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:978)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3647)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
我似乎无法弄清楚为什么它在打开数据库和崩溃应用程序时遇到问题。如果我删除该行
String[] MyNumbers = GatherNumbers();
阻止所有通话都没有问题。但是尝试中的那一行失败了。
答案 0 :(得分:0)
不应该是
Dbhelper dbHelper = new DatabaseHelper(context);
BTW上下文字段似乎总是为空。