2012-08-28 30 views
1

運行getWritableDatabase或getReadableDatabase時收到空錯誤。我知道這個問題已經提出了很多次,但是我花了7天的時間翻閱了所有的評論,沒有任何運氣。Android:獲取java.lang.NullPointerException與getWritableDatabase()

這是第一個代碼 - 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!"); 

這裏是在問題「如果」 statment,我讀到「空」的東西的答案很多,所以我試圖檢查其空以前生產SQL的任務,因爲你可以看到:

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 

我很樂意幫助任何可能的話,我真的卡住了!

+0

哪一部分的代碼調用DbHelper.OnCreate()?看起來你從ContentProvider複製了代碼 –

回答

0

我認爲你正在用一個活動的新調用初始化HandleCall。如果是這樣,你不能那樣做。因爲在這種情況下,你不會有服務HandleCall的上下文。這就是你碰撞的原因。

如果你正在做new HandleCall那麼你就不能在

CallData callData = new CallData(this); 

通過this你將不得不通過已在上述行被初始化的框架,而不是該適當context

編輯:

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


} 

而現在使用

CallData callData = new CallData(context); 

也同時呼籲new HandleCall(phone, Activityname.this)

+0

Ido怎麼做?我有「上下文背景」,我可以將「this」改爲「context」嗎? – user1630584

+0

除非你已經正確初始化它,否則你不能這樣做。檢查我的編輯 – nandeesh

+0

好吧,我打電話給它從「公共類MyPhoneStateListener擴展PhoneStateListener」,所以我不能做到這一點,我嘗試發送上下文而不是活動名稱,這並沒有工作 – user1630584

相關問題