1

我的启动接收器不断崩溃:

   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);       

    }   

数据库有记录:

我有一个按钮,它在 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 中使用的几乎相同。该按钮单击后的文本视图从数据库中显示(我上次测试):

1, 12120000 

它的天数和毫秒。

因此记录被存储在数据库中,并且可以检索它们,但是 OnBootReceiver 在启动时不断收到空指针异常,而他用光标和那个按钮做几乎相同的事情。

4

2 回答 2

1

首先,我会将您的所有代码移至另一个程序 -广播接收器最多运行 10 秒(当前)然后终止有关更多信息,请参阅此而这。任何未处理的代码都将保持不变。在执行数据库操作时理解这一点尤其重要。

您的空指针异常可能是您的光标,但您需要测试以确定什么是空的。在进行无法完成或引发错误的调用时(例如数据库操作),您可能还希望将代码包装在错误陷阱中。

只是一些建议可以帮助您解决问题并使代码更稳定。

于 2013-02-02T19:06:34.127 回答
1

如日志:

引起:java.lang.NullPointerException

因为 db 实例在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;
}

并且在使用它之前检查游标是否为onReceiveNULLBroadcastReceiver

于 2013-02-02T19:17:17.797 回答