[英]Fatal Exception thrown on Scheduler - rxJava2 in Android
I'm introducing myself to rxJava2 in Android with MVP. 我将自己介绍给具有MVP的Android rxJava2。
For initial steps I have created an Activity, a presenter and a Model objects (every of them with an interface class). 在最初的步骤中,我创建了一个Activity,一个presenter和一个Model对象(每个对象都有一个接口类)。
In my activity, I create the presenter, and try to load data, notifying the view when it is ready: 在我的活动中,我创建演示者,并尝试加载数据,并在视图准备就绪时通知它:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
MvpPresenter presenter = new Presenter(this,
AndroidSchedulers.mainThread());
presenter.loadData();
Inside the presenter, on loadData method, just calling back to view 在演示者内部,使用loadData方法,只需回调即可查看
@Override
public void loadData() {
compositeDisposable.add(dataRepository.getDataList().
subscribeOn(Schedulers.io()).
observeOn(mainScheduler).
subscribeWith(new DisposableSingleObserver<List<Book>>() {
@Override
public void onSuccess(@NonNull List<Book> bookList) {
if (!bookList.isEmpty())
view.displayBooks(bookList);
else
view.displayEmpty();
}
@Override
public void onError(@NonNull Throwable e) {
view.displayError("boooom");
}
}));
}
Method getBooks() is as follows (I have in mind to use an API, but so far, for simplicity purposes, I just have the data hardcoded) 方法getBooks()如下(我已经牢记要使用API,但到目前为止,为了简单起见,我只对数据进行了硬编码)
public Single<List<Shelf>> getShelfList() {
return Single.fromCallable(new Callable<List<Book>>() {
@Override
public List<Book> call() throws Exception {
Book b1 = new Book();
Book b2 = new Book();
Book b3 = new Book();
List<Book> bookList = Arrays.asList(b1,b2,b3);
return bookList;
}
});
}
The method view.displayBooks(bookList) just have a Toast message (again, for simplicity purposes) 方法view.displayBooks(bookList)仅具有一条Toast消息(同样,出于简单目的)
public void displayBooksInShelf(List<Shelf> shelfList) {
Toast.makeText(this, shelfList.size(), Toast.LENGTH_LONG).show();
}
Seems very simple and it should the Toast as three books are returned correctly... but when executing, it throws an exception: 看起来非常简单,应该正确地返回Toast,因为正确返回了三本书...但是执行时会抛出异常:
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:111)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
at android.content.res.Resources.getText(Resources.java:331)
at android.widget.Toast.makeText(Toast.java:287)
In Unitary Test (Mockito) everything seems to work fine, but together it fails. 在单一测试(Mockito)中,一切似乎都可以正常工作,但在一起却失败了。 My suspects are that there is some kind of thread issue.
我的怀疑是存在某种线程问题。 Anybody can help my with this problem?
有人可以帮助我解决这个问题吗?
Also, knowing that I'd like to use retrofit, is the approach I'm using the correct one? 另外,知道我想使用改造方法时,我使用的方法是否正确? Or maybe the thread should be managed in the Model/Repository class?
还是应该在Model / Repository类中管理线程?
Hope the information I've provided is enough to clarify where the problem is 希望我提供的信息足以澄清问题出在哪里
Thanks in advance! 提前致谢!
Notice this line in the stacktrace: 注意stacktrace中的这一行:
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
You are trying to access String
resource with invalid resource id. 您正在尝试使用无效的资源ID访问
String
资源。 Here where it happens: 在这里发生:
Toast.makeText(this, shelfList.size(), Toast.LENGTH_LONG).show();
The second argument must be String
message or int
resource id. 第二个参数必须是
String
消息或 int
资源ID。 You are passing int
here, so compiler chooses overload with a resource id (obviously invalid). 您在此处传递
int
,因此编译器选择带有资源ID的重载(显然无效)。
If you want to just print int
you have to pass String
representation of it: 如果只想打印
int
,则必须传递它的String
表示形式:
Toast.makeText(this, Integer.toString(shelfList.size()), Toast.LENGTH_LONG).show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.