我把手机连接到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
答案 0 :(得分:1)
你应该只使用上下文,用上下文替换上下文,你应该没问题。
答案 1 :(得分:1)
由于您的代码问题不明确,但有一些建议可供您使用。
1)您的变量milivreme
,milibaza
等必须是long类型而不是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);
答案 2 :(得分: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());
(问题是你在循环后使用分号)?
如果我的回答没有帮助,请给堆叠跟踪。