我遇到了RxJava,Retrofit和Multi-Window模式的问题......我在Activity中调用了我们自己的api和Retrofit(实际的代码比这复杂一点):
api.getEvent(...)
.subscribeOn(Schedulers.io())
.observeOn(AndroidScheduler.mainThread())
.subscribe(event -> setupUI(event),
throwable -> showSnackbar(throwable));
Run Code Online (Sandbox Code Playgroud)
当应用程序处于"正常"模式(全屏)时,一切运行正常...我可以将应用程序放入bg,将其放回前台并再次调用api调用方法(它位于onResume方法中 - kind并且UI被绘制没有问题.当我激活多窗口模式时,调用onResume,成功调用api方法,但永远不会到达subscribe()方法.这种情况的堆栈跟踪是:
Retrofit: java.io.InterruptedIOException: thread interrupted
at okio.Timeout.throwIfReached(Timeout.java:145)
at okio.Okio$1.write(Okio.java:77)
at okio.RealBufferedSink.flush(RealBufferedSink.java:221)
at com.squareup.okhttp.internal.framed.Http2$Writer.flush(Http2.java:381)
at com.squareup.okhttp.internal.framed.FramedConnection.newStream(FramedConnection.java:283)
at com.squareup.okhttp.internal.framed.FramedConnection.newStream(FramedConnection.java:249)
at com.squareup.okhttp.internal.http.Http2xStream.writeRequestHeaders(Http2xStream.java:135)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:710)
at com.facebook.stetho.okhttp.StethoInterceptor.intercept(StethoInterceptor.java:67)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:695)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:576)
at com.squareup.okhttp.Call.getResponse(Call.java:287)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205) at com.squareup.okhttp.Call.execute(Call.java:80)
at com.newrelic.agent.android.instrumentation.okhttp2.CallExtension.execute(CallExtension.java:43)
at retrofit.client.OkClient.execute(OkClient.java:53)
at com.newrelic.agent.android.instrumentation.retrofit.ClientExtension.execute(ClientExtension.java:42)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)
at retrofit.RxSupport$2.run(RxSupport.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:761)
Run Code Online (Sandbox Code Playgroud)
应用程序不会崩溃但UI仍处于"占位符"状态,加载ProgressBar将永远消失.
编辑:我在其他活动中有相同的代码结构.但问题并没有出现在其他地方.
我想访问在方法中使用gestureListener的视图GestureListener本身.这是我的倾听者:
public class GestureSwipeListener extends SimpleOnGestureListener {
final Context myContext;
// Costanti per lo swipe
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
public GestureSwipeListener(Context context) {
myContext = context;
}
@Override
public boolean onDown(MotionEvent e) {
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) …Run Code Online (Sandbox Code Playgroud) 我想为我的Presenter类创建一个测试但是我在Presenter本身内部遇到了CompositeSubscription实例的问题.当我运行测试时,我收到此错误:
java.lang.NullPointerException
at rx.subscriptions.CompositeSubscription.add(CompositeSubscription.java:60)
at com.example.Presenter.addSubscription(Presenter.java:67)
at com.example.Presenter.getGummyBears(Presenter.java:62)
Run Code Online (Sandbox Code Playgroud)
这大致是我的Presenter类:
public class Presenter {
CompositeSubscription compositeSubscription = new CompositeSubscription();
//creation methods...
public void addSubscription(Subscription subscription) {
if (compositeSubscription == null || compositeSubscription.isUnsubscribed()) {
compositeSubscription = new CompositeSubscription();
}
compositeSubscription.add(subscription);
}
public void getGummyBears() {
addSubscription(coreModule.getGummyBears());
}
}
Run Code Online (Sandbox Code Playgroud)
CoreModule是一个接口(不同模块的一部分),还有另一个类CoreModuleImpl,其中包含所有改进API调用及其转换为Subscriptions.就像是:
@Override public Subscription getGummyBears() {
Observable<GummyBears> observable = api.getGummyBears();
//a bunch of flatMap, map and other RxJava methods
return observable.subscribe(getDefaultSubscriber(GummyBear.class));
//FYI the getDefaultSubscriber method posts a GummyBear event on EventBus
}
Run Code Online (Sandbox Code Playgroud)
现在我想做的是测试getGummyBears() …