2013-02-02 141 views
1

我在開機接收器總是崩潰:的Android OnBootReceiver崩潰異常空指針

01-06 03:20:13.861: E/AndroidRuntime(15832): FATAL EXCEPTION: main 
01-06 03:20:13.861: E/AndroidRuntime(15832): java.lang.RuntimeException: Unable to start receiver com.example.prva.OnBootReceiver: java.lang.NullPointerException 
01-06 03:20:13.861: E/AndroidRuntime(15832): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2043) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at android.app.ActivityThread.access$2400(ActivityThread.java:132) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1098) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at android.os.Handler.dispatchMessage(Handler.java:99) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at android.os.Looper.loop(Looper.java:143) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at android.app.ActivityThread.main(ActivityThread.java:4277) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at java.lang.reflect.Method.invokeNative(Native Method) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at java.lang.reflect.Method.invoke(Method.java:507) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at dalvik.system.NativeStart.main(Native Method) 
01-06 03:20:13.861: E/AndroidRuntime(15832): Caused by: java.lang.NullPointerException 
01-06 03:20:13.861: E/AndroidRuntime(15832): at com.example.prva.DatabaseManager.getAllData(DatabaseManager.java:91) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at com.example.prva.OnBootReceiver.onReceive(OnBootReceiver.java:32) 
01-06 03:20:13.861: E/AndroidRuntime(15832): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2028) 
01-06 03:20:13.861: E/AndroidRuntime(15832): ... 10 more 

廣播接收器:

@Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 
     Calendar c2 = Calendar.getInstance();    
     int hour = c2.get(Calendar.HOUR_OF_DAY); 
     int minute = c2.get(Calendar.MINUTE); 
     int sek = c2.get(Calendar.SECOND); 

     int dan; 
     int dodaj; 

     milivreme = ((hour * 60 * 60 * 1000)+ (minute * 60 * 1000) + (sek * 1000)); 

     Cursor cursor = DatabaseManager.getAllData(); 

     cursor.moveToFirst(); 
     if (!cursor.isAfterLast()) 
     { 
      do 
      {    
     milibaza = cursor.getInt(2); 
     razlika = milibaza - milivreme; 

     Intent intent1 = new Intent(context, AlarmReceiver.class); 
     PendingIntent pendingintent = PendingIntent.getService(context, 3, intent1, PendingIntent.FLAG_CANCEL_CURRENT); 
     AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE); 

     am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + razlika, pendingintent); 
      } 
      while (cursor.moveToNext()); 
     } 
     cursor.close(); 
    } 

的getAllData方法:

public static Cursor getAllData() { 

     return db.query(TABLE_NAME, new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO }, 
       null, null, null, null, null);  

    } 

DATABSE有記錄:

我有顯示最後一個值從數據庫中一個TextView按鈕:

public void onClick(View v) { 
       // TODO Auto-generated method stub 
       String BazaDan; 
       int BazaVrijeme; 
       DatabaseManager.Cursoric(); 

       BazaDan = DatabaseManager.getDan(); 
       BazaVrijeme = DatabaseManager.getVrijeme(); 
       textbaza.setText(new StringBuilder(BazaDan).append(", ").append(BazaVrijeme).toString()); 
      } 
     }); 

方法光標該按鈕使用:

public static void Cursoric(){ 

     Cursor cursor; 
     cursor = db.query 
       (
         TABLE_NAME, 
         new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },      
         null, null, null, null, null 
       ); 
     cursor.moveToFirst(); 
     if (!cursor.isAfterLast()) 
     { 
      do 
      { 
       BazaDan = cursor.getString(1); 
       BazaVrijeme = cursor.getInt(2); 
      } 
      while (cursor.moveToNext()); 
     }  

     cursor.close(); 
    } 

正如你可以看到它幾乎我同樣的事情在BroadcastReceiver中使用。 從數據庫中該按鈕點擊後顯示TextView的(我最後一次測試):

1, 12120000 

它的天數,和毫秒。

所以記錄被存儲在數據庫中,他們可以檢索但OnBootReceiver不斷引導他幾乎做同樣的事情用光標作爲按鈕而當越來越空指針異常。

+0

其中DatabaseManager類我認爲創建或訪問數據庫,我們就需要通過上下文數據庫類,因此確保你傳遞?和PLZ後完整的日誌 –

+0

@ρяσѕρєяK完整的日誌發佈。我的DatabaseManager類創建並處理數據庫(我調用getAllData的那些方法來自它)。 – user1880779

+0

什麼是DatabaseManager類中的91行號碼? –

回答

1

在日誌中:

產生的原因:顯示java.lang.NullPointerException

因爲數據庫實例裏面getAllData法空,因此使用它像以前一樣只是讓空校驗:

public static Cursor getAllData() 
{ 
     Cursor cursor=null; 
    if(db!=null) 
    { 
     cursor=db.query(TABLE_NAME, new String[] 
       { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, 
       TABLE_COLUMN_TWO }, null, 
       null, null, null, null); 
    } 

return cursor; 
} 

並且在使用它之前也檢查光標爲NULL onReceive方法BroadcastReceiver

1

首先,我會將您的所有代碼移動到另一個程序 - 廣播接收器最多運行10秒(當前),然後終止See this for more informationAnd This。任何未處理的代碼將保持如此。這對於理解執行數據庫操作時尤爲重要。

你的空指針異常可能是你的光標,但你需要測試,以確定哪些是空的。在進行無法完成或發生錯誤的調用時(例如數據庫操作),您也可能想要將代碼包裝在錯誤陷阱中。

只是一些建議,幫助您解決問題,使你的代碼更加穩定。

+0

謝謝你的提示 – user1880779