之前一直用RxLifecycle 也没啥事,但是在引入EventBus之后引发了一个异常:java.lang.NoClassDefFoundError: android/os/PersistableBundle ,这个异常刚开始没发现,因为开始时使用的时高版本测试的,但是当打包准备发布时测试,发现在4.4机子上直接崩溃了
02-03 09:02:01.527 4190-4190/com.u91porn E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.u91porn, PID: 4190
java.lang.NoClassDefFoundError: android/os/PersistableBundle
at java.lang.Class.getDeclaredMethods(Native Method)
at java.lang.Class.getPublicMethodsRecursive(Class.java:894)
at java.lang.Class.getMethods(Class.java:877)
at org.greenrobot.eventbus.SubscriberMethodFinder.findUsingReflectionInSingleClass(SubscriberMethodFinder.java:157)
at org.greenrobot.eventbus.SubscriberMethodFinder.findUsingInfo(SubscriberMethodFinder.java:88)
at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:64)
at org.greenrobot.eventbus.EventBus.register(EventBus.java:140)
at com.u91porn.ui.main.MainActivity.onCreate(MainActivity.java:110)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.os.PersistableBundle" on path: DexPathList[[zip file "/data/app/com.u91porn-1.apk", zip file "/data/data/com.u91porn/code_cache/secondary-dexes/com.u91porn-1.apk.classes2.zip"],nativeLibraryDirectories=[/data/app-lib/com.u91porn-1, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at java.lang.Class.getDeclaredMethods(Native Method)
at java.lang.Class.getPublicMethodsRecursive(Class.java:894)
at java.lang.Class.getMethods(Class.java:877)
at org.greenrobot.eventbus.SubscriberMethodFinder.findUsingReflectionInSingleClass(SubscriberMethodFinder.java:157)
at org.greenrobot.eventbus.SubscriberMethodFinder.findUsingInfo(SubscriberMethodFinder.java:88)
at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:64)
at org.greenrobot.eventbus.EventBus.register(EventBus.java:140)
at com.u91porn.ui.main.MainActivity.onCreate(MainActivity.java:110)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
于是查看日志似乎是引入eventbus引发的,于是老套路,copy异常google,可是发现解决问题都是大同小异,重写oncreate方法问题,但对我貌似没啥用??在仔仔细细检查了自己重写的activiy的oncreate方法无误后,一脸无助了……
于是细想,既然自己的没问题,那么问题应该时出在继承第三方activiy的,于是逐个排除,在查到RxLifecycle 时,终于时发现了其重写了onCreate(Bundle savedInstanceState, PersistableBundle persistentState) 有两个参数的方法,虽然我自己本身并没有重写,但是也会抛出异常,想着也不能去copy下来改了吧,麻烦,且治标不治本,于是想到了不久前google发布了Lifecycle组件,似乎不用继承即可感知activiy以及fragment的生命周期,于是google Lifecycle组件相关资料以及在RxLifecycle 中查找,发现原来RxLifecycle早支持了呀。
于是乎在项目中引入RxLifecycle 再也不用继承它原来提供的那些基类了,只需
compile 'com.trello.rxlifecycle2:rxlifecycle:2.2.1'
// If you want to use Android Lifecycle for providers
compile 'com.trello.rxlifecycle2:rxlifecycle-android-lifecycle:2.2.1'
public class MainActivity extends AppCompatActivity{
protected final LifecycleProvider provider = AndroidLifecycle.createLifecycleProvider(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//然后调用即可
myObservable.compose(provider.bindUntilEvent(Lifecycle.Event.ON_STOP))
}
}
就是这么简单,收工(当然也许是我自己使用方式不当引起,但是这反倒可以引出使用新方式,也是不错的了)。