我正在使用以下代码,
public class Live extends ActionBarActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_live);
Long time = new GregorianCalendar().getTimeInMillis()+10*1000;
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmIntent=PendingIntent.getBroadcast(this, 1, intentAlarm, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,time, alarmIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1000 * 10, alarmIntent);
Toast.makeText(this, "Alarm Manager calling....", Toast.LENGTH_LONG).show();
}
在我的清单文件中,我也包括以下行
<receiver android:name=".AlarmReceiver"/>
我的AlarmReceiver Class只显示Toast
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context,"Alarm ringing",Toast.LENGTH_SHORT).show();
}
}
所以当我启动Application时,它会给我空指针异常Log cat
05-07 14:27:49.192: E/AndroidRuntime(17793): FATAL EXCEPTION: main
05-07 14:27:49.192: E/AndroidRuntime(17793): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.livestream/com.example.livestream.Live}: java.lang.NullPointerException
05-07 14:27:49.192: E/AndroidRuntime(17793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
05-07 14:27:49.192: E/AndroidRuntime(17793): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
05-07 14:27:49.192: E/AndroidRuntime(17793): at android.app.ActivityThread.access$700(ActivityThread.java:143)
05-07 14:27:49.192: E/AndroidRuntime(17793): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
05-07 14:27:49.192: E/AndroidRuntime(17793): at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 14:27:49.192: E/AndroidRuntime(17793): at android.os.Looper.loop(Looper.java:137)
05-07 14:27:49.192: E/AndroidRuntime(17793): at android.app.ActivityThread.main(ActivityThread.java:4960)
05-07 14:27:49.192: E/AndroidRuntime(17793): at java.lang.reflect.Method.invokeNative(Native Method)
05-07 14:27:49.192: E/AndroidRuntime(17793): at java.lang.reflect.Method.invoke(Method.java:511)
05-07 14:27:49.192: E/AndroidRuntime(17793): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-07 14:27:49.192: E/AndroidRuntime(17793): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-07 14:27:49.192: E/AndroidRuntime(17793): at dalvik.system.NativeStart.main(Native Method)
05-07 14:27:49.192: E/AndroidRuntime(17793): Caused by: java.lang.NullPointerException
05-07 14:27:49.192: E/AndroidRuntime(17793): at com.example.livestream.Live.onCreate(Live.java:63)
05-07 14:27:49.192: E/AndroidRuntime(17793): at android.app.Activity.performCreate(Activity.java:5203)
05-07 14:27:49.192: E/AndroidRuntime(17793): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-07 14:27:49.192: E/AndroidRuntime(17793): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
05-07 14:27:49.192: E/AndroidRuntime(17793): ... 11 more
你能告诉我哪里出错了吗?
答案 0 :(得分:0)
您应该尝试删除set
方法
alarmManager.set(AlarmManager.RTC_WAKEUP,time, alarmIntent);
并仅使用setRepeating
(如果您想重复该服务,否则请删除setRepeating)
在此之后,您必须创建一个扩展IntentService
的类并插入方法onHandleIntent
@Override
protected void onHandleIntent(Intent intent) {
..code here.. (toast example)
}
之后,在Receiver类上,重写onReceiver
方法并插入调用服务的意图
Intent i = new Intent(context,IntentService);
context.startService(i);
在最后一步中,在Manifest中添加服务信息(如接收者)
<service android:name=".name of your class"
android:exported="true" /> <-I write also this