1

我的手机已连接到 Eclipse,但在手机“完全”启动之前我看不到 logcat,并且我的接收器(应用程序)在此之前崩溃,所以我无法调试它。我确信它必须与上下文和意图有关:

由于上下文问题,我的接收器在启动时崩溃了,我敢肯定:

 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(3);
    razlika = milibaza - milivreme;

    Intent intent1 = new Intent(context, AlarmReceiver.class);
    PendingIntent pendingintent = PendingIntent.getService(context, 2, 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());
    }
}   

更新,终于设法捕捉到异常:

01-06 02:21:19.920: E/AndroidRuntime(7360): java.lang.RuntimeException: Unable to start receiver com.example.prva.OnBootReceiver: java.lang.NullPointerException
4

3 回答 3

1

由于您的代码尚不清楚问题,但有一些建议给您。

1)您的变量milivrememilibaza必须是长类型而不是int。

2)而不是下面的代码,只使用milivreme = System.currentTimeMillis();它以毫秒为单位给出当前时间。

    Calendar c = Calendar.getInstance();                
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    int sek = c.get(Calendar.SECOND);
    int dan;
    int dodaj;

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

3)检查游标是否不为空。前cursor.moveToFirst();

4)看到这一行>>

PendingIntent pendingintent = PendingIntent.getService(context, 2, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
于 2013-02-02T14:53:34.417 回答
1

你应该只使用上下文,用上下文替换上下文,你应该没问题。

于 2013-02-02T14:45:49.503 回答
0

你应该使用Context context,不要Context contextic

private Context contextic;    //    NEEDLEES !
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Calendar c = Calendar.getInstance();                
    int hour = c.get(Calendar.HOUR_OF_DAY);
    int minute = c.get(Calendar.MINUTE);
    int sek = c.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(3);
    razlika = milibaza - milivreme;

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

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

我问你的问题:你为什么写while (cursor.moveToNext());(问题是你在循环后使用分号)?

如果我的回答没有帮助,请提供堆栈跟踪。

于 2013-02-02T14:54:10.733 回答