我正在处理Android上的这个问题:
错误线程访问Realm。 Realm对象只能在创建它们的线程上访问。
我想在我的RemoteViewsFactory中使用Realm。
public class RemoteViewsX implements RemoteViewsFactory {
public RemoteViews getViewAt(int paramInt) {
if (this.results != null && this.results.size() != 0 && this.results.get(paramInt) != null) {
//FAILED HERE
}
这个调用失败了!为什么?
我在我的类中以以下方式获取数据:
public void onDataSetChanged() {
Realm realm = Realm.getInstance(RemoteViewsX.this.ctx);
this.results = realm.where(Model.class).findAll();
}
我像这样调用了我的remoteFactory:
public class ScrollWidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return new RemoteViewsX (getApplicationContext());
}
}
有什么想法吗?
onDataSetChanged
方法是从另一个线程调用的,而不是你的getViewAt
方法。我不确定这是因为RemoteViewsService的工作方式还是你的代码。 - Christian Melchior