当我尝试在广播接收器中使用LockNow()时,似乎出现错误。任何人都可以帮助我。
public class Onlockreceive extends BroadcastReceiver {
LockSettings lockactivity;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
lockactivity.mdevicepolicymanager.lockNow();
}
}
错误消息:
12-31 03:14:16.322:D / AndroidRuntime(27478):关闭VM 12-31 03:14:16.322:W / dalvikvm(27478):threadid = 1:线程退出未捕获的异常(group = 0x41b0d378)12-31 03:14:16.332:E / AndroidRuntime(27478):FATAL EXCEPTION:main 12-31 03:14:16.332:E / AndroidRuntime(27478):java.lang.RuntimeException:无法启动接收器com。 example.settings2.Onlockreceive:java.lang.NullPointerException 12-31 03:14:16.332:E / AndroidRuntime(27478):at android.app.ActivityThread.handleReceiver(ActivityThread.java:2257)12-31 03:14:16.332 :E / AndroidRuntime(27478):在android.app.ActivityThread.access $ 1500(ActivityThread.java:138)12-31 03:14:16.332:E / AndroidRuntime(27478):在android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1283)12-31 03:14:16.332:E / AndroidRuntime(27478):在android.os.Handler.dispatchMessage(Handler.java:99)12-31 03:14:16.332:E / AndroidRuntime (27478):在android.os.Looper.loop(Looper.java:213)12-31 03:14: 16.332:E / AndroidRuntime(27478):在android.app.ActivityThread.main(ActivityThread.java:4787)12-31 03:14:16.332:E / AndroidRuntime(27478):at java.lang.reflect.Method.invokeNative (Native Method)12-31 03:14:16.332:E / AndroidRuntime(27478):at java.lang.reflect.Method.invoke(Method.java:511)12-31 03:14:16.332:E / AndroidRuntime( 27478):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:789)12-31 03:14:16.332:E / AndroidRuntime(27478):at com.android.internal.os.ZygoteInit .main(ZygoteInit.java:556)12-31 03:14:16.332:E / AndroidRuntime(27478):at dalvik.system.NativeStart.main(Native Method)12-31 03:14:16.332:E / AndroidRuntime( 27478):引起:java.lang.NullPointerException 12-31 03:14:16.332:E / AndroidRuntime(27478):at com.example.settings2.Onlockreceive.onReceive(Onlockreceive.java:15)12-31 03:14 :16.332:E / AndroidRuntime(27478):在android.app.ActivityThread.handleReceiver(ActivityThread.java:2250)12- 31 03:14:16.332:E / AndroidRuntime(27478):... 10 more
我的问题类似于这个same problem ..但我不明白如何解决它
这是我用来调用广播接收器的代码
private void setupAlarm(int seconds,boolean s) {
// Finish the currently running activity
// MainActivity.this.finish();
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(getBaseContext(), Onlockreceive.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
LockSettings.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Log.d(TAG, "Setup the alarm");
// Getting current time and add the seconds in it
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, seconds);
if (s == true){
//alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
//alarmManager.setInexactRepeating(type, triggerAtMillis, intervalMillis, operation)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, pendingIntent );
}else if (s == false)
{
alarmManager.cancel(pendingIntent);
}
}
答案 0 :(得分:0)
lockactivity
或mdevicepolicymanager
为null
。这并不奇怪,因为您的过程很可能在BroadcastReceiver
获得控制之前终止。
静态数据成员只是缓存。不要依赖他们。大多数情况下不会将Activity
放在静态数据成员中,因为这样做会泄漏大量内存。
您可以使用传递给Context
的{{1}}来检索onReceive()
并致电DevicePolicyManager
。
<强>更新强>
这是修订后的类,包含更好的格式和案例:
lockNow()