1

运行 getWritableDatabase 或 getReadableDatabase 时收到空错误。我知道这个问题已被多次提出,但我花了 7 天时间倾注所有评论,但没有任何运气。

这是第一个代码 - HandleCall

package com.mintz.callmeback;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class HandleCall extends Service implements Runnable {
String TAG = "HandleCall";
String phoneNum;

private NotificationManager notificationManager; 
private Notification notification;


//CallMeBackApp CallApp = new CallMeBackApp();
Context context;



public HandleCall(String phone)
{
    phoneNum = phone;

    Log.i(TAG, "Constractor");


}

public void run() {

    CallData callData = new CallData(this);

    //CallMeBackApp callApp = new CallMeBackApp();
    //callApp.endCall2();
    Log.i(TAG, "run1"+phoneNum);
    if (callData!=null)
        Log.d(TAG,"callData is not null!");

这是“if”语句中的问题,我阅读了很多关于“null”的答案,所以我尝试在 SQL 任务之前检查它是否为 null,如您所见:

    if ((callData != null)&&(callData.isPhoneInTheList1(phoneNum, this))){              Log.i(TAG, "run2");
        SendSms.sendIt(phoneNum);
        Log.i(TAG, "run3");

        notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
        notification = new Notification(android.R.drawable.stat_notify_chat,"", 0);
        sendCallNotification(phoneNum);
    }
    else{
        Log.d(TAG, "didnt found the phone number");
    }

}

private void sendCallNotification(String phone) {
    Log.d(TAG, "sendCallNotification'ing");
    Intent callIntent = new Intent(getApplicationContext(), MakeCall.class);
    callIntent.putExtra("PhoneNumberToCallBack", phone);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, -1,callIntent ,PendingIntent.FLAG_UPDATE_CURRENT); //
    this.notification.when = System.currentTimeMillis(); // time when it's happened
    this.notification.flags |= Notification.FLAG_AUTO_CANCEL; // This flag tells the Notification manager to cancel this notification as soon as the user clicks on it.
    CharSequence notificationTitle = this.getText(R.string.callBackToTitle); //
    CharSequence notificationSummary = this.getString(R.string.callBackToMessage, phone);
    this.notification.setLatestEventInfo(this, notificationTitle, notificationSummary, pendingIntent); //
    this.notificationManager.notify(0, this.notification);
    Log.d(TAG, "sendTimelineNotificationed");
    }

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}


}

和另一个类:

package com.mintz.callmeback;

import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class CallData {


static final String TAG = "CallData";
static final String DB_NAME = "PhoneToCallBack.db"; // database filename
static final int DB_VERSION = 1;
static final String TABLE = "PhoneToCallBack";
static final String C_ID = "_id";
static final String C_PHONE_NUMBER = "phone_number";
private static final String GET_ALL_ORDER_BY = C_PHONE_NUMBER + " DESC";
private static final String[] DB_PHONE_COLUMNS = { C_PHONE_NUMBER };
Context context;

// Constructor
public CallData(Context context) {
    Log.i(TAG, "Initialized data");
    this.context = context;
    this.dbHelper = new DBHelper(context);
}

// DataBase Helper for the all App
public class DBHelper extends SQLiteOpenHelper {
    Context context;


    // Constructor
      public DBHelper(Context context) {
          super(context, DB_NAME, null, DB_VERSION);
          this.context = context;
      }  
    // Not nead for now
    //public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
    //  super(context, name, factory, DB_VERSION);
    //  this.context = context;
    //}

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG, "Creating database: " + DB_NAME);
        //String sql = context.getString(R.string.sql1);
        //Log.d(TAG, "onCreated sql: " + sql);
        db.execSQL("create table " + TABLE + " (" + C_ID + " int primary key, "
                  + C_PHONE_NUMBER + " text)");
        //db.execSQL(sql);
    }


    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop table if exists " + TABLE); // blow the old database away
        Log.d(TAG, "onUpdated");
        this.onCreate(db); // run onCreate to get new database
      }
}//End of DBHelper Class



public DBHelper dbHelper;

public void close() {
    this.dbHelper.close();
}


public void insertOrIgnore(ContentValues values) {
    Log.d(TAG, "insertOrIgnore on " + values);
    SQLiteDatabase db = this.dbHelper.getWritableDatabase();
    try {
        db.insertWithOnConflict(TABLE, null, values, SQLiteDatabase.CONFLICT_IGNORE); 
    } finally {
        db.close();
    }
}

  /**
   * 
   * @return Cursor where the columns are _id, phone_number
   */
  public Cursor getPhoneList() {
    SQLiteDatabase db = this.dbHelper.getReadableDatabase();
    return db.query(TABLE, null, null, null, null, null, GET_ALL_ORDER_BY);
  } 

  /**
   * Deletes ALL the data
   */
  public void delete() {
    // Open Database
    SQLiteDatabase db = this.dbHelper.getWritableDatabase();

    // Delete the data
    db.delete(TABLE, null, null);

    // Close Database
    db.close();
  }

  /**
   * 
   * @param Phone number to check if exist
   * @return True if the number is in the list
   */
  public Boolean isPhoneInTheList(String phone) {
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Log.d(TAG, "befor query");
    //Cursor cursor = db.query(TABLE, null, null, phone, null, null, null);

    Cursor cursor = db.query("PhoneToCallBack", null, "phone_number=" + phone,null, null, null, null);


      return (cursor.moveToFirst()) ; //Return True if find the Phone number in the DB


  }

  public Boolean isPhoneInTheList1(String phone, Context context) {
        //DBHelper dbHelper1;
        dbHelper = new DBHelper(context);
        SQLiteDatabase database;

        if (dbHelper != null)
            Log.d(TAG,"dbHelper1 != null");

        database = dbHelper.getWritableDatabase();
        Log.d(TAG, "befor query");
        Cursor cursor = database.query("PhoneToCallBack", null, "phone_number=" + phone,null, null, null, null);
        return (cursor.moveToFirst()) ; //Return True if find the Phone number in the DB


      }

}

这是我拨打电话并拨打 HandleCall 时得到的信息:

08-28 15:17:45.706: I/CallData(340): Initialized data
08-28 15:17:45.725: I/CallData(340): Initialized data
08-28 15:17:45.725: D/CallMeBackMainActivity(340): onCreate
08-28 15:17:45.826: D/CallMeBackMainActivity(340): onResume
08-28 15:17:45.826: D/CallMeBackMainActivity(340): Get the data from the database
08-28 15:17:45.886: D/CallMeBackMainActivity(340): Get the Cursor
08-28 15:19:21.495: D/PhoneServiceReceiver(340): the number is: 55555555
08-28 15:19:21.505: D/MyPhoneStateListener(340): RINGING from 55555555
08-28 15:19:21.535: I/HandleCall(340): Constractor
08-28 15:19:21.588: I/CallData(340): Initialized data
08-28 15:19:21.588: I/HandleCall(340): run155555555
08-28 15:19:21.595: D/HandleCall(340): callData is not null!
08-28 15:19:21.605: D/CallData(340): dbHelper1 != null
08-28 15:19:21.605: W/dalvikvm(340): threadid=9: thread exiting with uncaught exception (group=0x40015560)
08-28 15:19:21.638: E/AndroidRuntime(340): FATAL EXCEPTION: Thread-10
08-28 15:19:21.638: E/AndroidRuntime(340): java.lang.NullPointerException
08-28 15:19:21.638: E/AndroidRuntime(340):  at                android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
08-28 15:19:21.638: E/AndroidRuntime(340):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
08-28 15:19:21.638: E/AndroidRuntime(340):  at com.mintz.callmeback.CallData.isPhoneInTheList1(CallData.java:133)
08-28 15:19:21.638: E/AndroidRuntime(340):  at com.mintz.callmeback.HandleCall.run(HandleCall.java:43)
08-28 15:19:21.638: E/AndroidRuntime(340):  at java.lang.Thread.run(Thread.java:1019)
08-28 15:19:21.895: W/IInputConnectionWrapper(340): showStatusIcon on inactive     InputConnection
08-28 15:19:40.655: D/dalvikvm(340): GC_EXPLICIT freed 92K, 52% free 2599K/5379K, external 884K/1038K, paused 115ms

我很想得到任何帮助,我真的被困住了!

4

1 回答 1

0

我认为您正在使用来自 Activity 的新调用来初始化 HandleCall。如果是这样,你不能那样做。因为在这种情况下,您将没有服务 HandleCall 的上下文。这就是为什么你有一个崩溃。

如果您正在这样做,那么new HandleCall您将无法通过this

CallData callData = new CallData(this);

您必须context在上面的行中传递一个已由框架初始化的属性,而不是 this

编辑:

public HandleCall(String phone, Context ctx)
{
    phoneNum = phone;
    context = ctx;
    Log.i(TAG, "Constractor");


}

现在使用

 CallData callData = new CallData(context);

还有打电话的时候new HandleCall(phone, Activityname.this)

于 2012-08-28T15:43:48.633 回答