简体   繁体   English

使用observeOn(),Android UI线程崩溃了我的模拟器测试

[英]Using observeOn() the Android UI thread crashes my emulator testing

When I use RxAndroid and .observeOn(AndroidSchedulers.mainThread()) , and run tests on an emulator using Android Studio, the whole test run crashes with: 当我使用RxAndroid和.observeOn(AndroidSchedulers.mainThread()) ,并使用Android Studio在模拟器上运行测试时,整个测试运行崩溃:

Instrumentation run failed due to ' java.lang.NoSuchMethodError ' 由于' java.lang.NoSuchMethodError '导致仪表运行失败

and logcat output like the following: logcat输出如下:

FATAL EXCEPTION: main
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:54)
       at android.os.Handler.handleCallback(Handler.java:587)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:123)
       at android.app.ActivityThread.main(ActivityThread.java:4627)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:521)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
       at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodError: rx.internal.schedulers.ScheduledAction.lazySet
       at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:46)
       ... 9 more
Error in app net.tbmcv.rxtest running instrumentation ComponentInfo{net.tbmcv.rxtest.test/android.test.InstrumentationTestRunner}:
  java.lang.NoSuchMethodError
  java.lang.NoSuchMethodError: rx.internal.schedulers.ScheduledAction.lazySet
Force stopping package net.tbmcv.rxtest uid=10042

Neither Android Studio nor the logcat output mention any lines of code in my project or my tests. Android Studio和logcat输出都没有提到我的项目或测试中的任何代码行。 Here are some relevant parts of a short example project I created that reproduces this crash: 以下是我创建的一个简短示例项目的一些相关部分,用于重现此崩溃:

app/build.gradle

dependencies {
    compile 'io.reactivex:rxandroid:0.24.0'
}

MainActivity.java

public class MainActivity extends Activity {
    public void setTitleFrom(Observable<String> observable) {
        observable.observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        setTitle(s);
                    }
                });
    }
}

MainActivityFuncTest.java

public class MainActivityFuncTest extends ActivityInstrumentationTestCase2<MainActivity> {
    public void testSetTitle() {
        final String newTitle = "Hello World";
        getActivity().setTitleFrom(Observable.just(newTitle));
        getInstrumentation().waitForIdleSync();
        assertEquals(newTitle, getActivity().getTitle());
    }
}

My test runs correctly if I modify my MainActivity code to not use observeOn() : 如果我将MainActivity代码修改为不使用observeOn()我的测试会正确运行:

public void setTitleFrom(Observable<String> observable) {
    observable.subscribe(new Action1<String>() {
        @Override
        public void call(final String s) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    setTitle(s);
                }
            });
        }
    });
}

But I'd rather use observeOn() , because it's cleaner and more modular. 但我宁愿使用observeOn() ,因为它更清洁,更模块化。 How can I get my tests to run? 我怎样才能让我的测试运行?

My emulator is running API version 8 (Android 2.2). 我的模拟器正在运行API版本8(Android 2.2)。

According to RxJava's GitHub page , RxJava versions 1.0.x only work on Android 2.3 or later (corresponding to API 9 or later). 根据RxJava的GitHub页面 ,RxJava版本1.0.x仅适用于Android 2.3或更高版本(对应于API 9或更高版本)。 RxAndroid depends on RxJava, of course, so the same restriction applies (even though it's currently not mentioned on the RxAndroid page.) 当然,RxAndroid依赖于RxJava,因此适用相同的限制(即使它目前在RxAndroid页面上没有提及。)

The above tests run fine on an emulator running a more recent version of Android. 上述测试在运行更新版Android的模拟器上运行良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM