在service中的BroadcasReceiver遇到no empty constructor错误

663 查看

今天在service中写了个内部广播接收器,结果运行的时候出现下面的问题:

Process: com.lu.times.times, PID: 7551
    java.lang.RuntimeException: Unable to instantiate receiver com.lu.times.service.TimesService$LockScreenReceiver: java.lang.InstantiationException: can't instantiate class com.lu.times.service.TimesService$LockScreenReceiver; no empty constructor
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:2823)
            at android.app.ActivityThread.access$1700(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5872)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.InstantiationException: can't instantiate class com.lu.times.service.TimesService$LockScreenReceiver; no empty constructor
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1208)
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:2818)
            at android.app.ActivityThread.access$1700(ActivityThread.java:156)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5872)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)

其实很简单啦,内部的broadcastReceiver需要被动态注册,或者是static类。
这样就OK了,我比较喜欢~

 private void registerReceiver(){
        xxxReceiver = new xxxReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("xxxx");    
  }