Android 领域错误,尝试使用异步。说它是从没有循环器的线程中打开的

Android error with realm, trying to use async. Says it's opened from a thread with no looper

我在这段代码中收到以下错误: "Your Realm is opened from a thread without a Looper. Async queries need a Handler to send results of your query" 在这一行:

"RealmResults<UserVehicle> completed = realm.where(UserVehicle.class).equalTo("id", userVehicleID).findAllAsync();"

我尝试在 Handler(Looper.getMain()) 中设置 realm.executeTransaction 但没有成功。我做错了什么?

PSVehicleService.getInstance(PSVehicleDetailsActivity.this).fetchVehicleTrips(userVehicle, fiveWeeksBeforeDate, lastTripDate, userID, new JsonCallback() {
            @Override
            public void onResponse(final JSONObject jsonObject, VolleyError error) {
                if(jsonObject != null) {
                    Log.i("","testVehicles maybeFetchData 11");
                    realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
                            try {
                                ObjectMapper mapper = new ObjectMapper();
                                final List<Trip> trips = mapper.readValue(jsonObject.getJSONArray("trips").toString(), new TypeReference<List<Trip>>() {
                                });
                                Log.i("","testVehicles maybeFetchData 12");
                                RealmResults<UserVehicle> completed = realm.where(UserVehicle.class).equalTo("id", userVehicleID).findAllAsync();
                                UserVehicle userVehicle = null;
                                if(completed.size() > 0){
                                    userVehicle = completed.get(0);
                                }
                                if(userVehicle != null) {
                                    Log.i("", "testVehicles maybeFetchData 13");
                                    userVehicle.getTrips().clear();
                                    Log.i("", "testVehicles maybeFetchData 14");
                                    userVehicle.getTrips().addAll(trips);
                                    Log.i("", "testVehicles maybeFetchData 15");
                                    realm.copyToRealmOrUpdate(userVehicle);
                                    Log.i("", "testVehicles maybeFetchData 16");
                                }else{
                                    Log.i("", "testVehicles maybeFetchData 16 NULLLLLLLL");
                                }
                            } catch (Exception e) {
                                Log.i("", "fetchTripsSinceWeeksInPast fetchVehicleTrips2 error" + e.getMessage());
                                Utils.appendLog("ERROR fetchVehicleTrips is:" + e.getMessage(), true);
                            }
                        }
                    }, new Realm.Transaction.Callback() {
                        @Override
                        public void onSuccess() {
                            Log.i("","testVehicles maybeFetchData 17");
                            setPager();
                            Log.i("","testVehicles maybeFetchData 18");
                        }

                        @Override
                        public void onError(Exception e) {
                            Log.i("","testVehicles maybeFetchData 18 ERROR:" + e.getMessage());
                        }
                    });
                }
            }
        });

问题是您在异步事务中使用了异步查询——这根本没有必要。

当调用realm.executeTransaction(Realm.Transaction, Realm.Transaction.Callback)时,事务中的代码块将运行在没有looper且不需要looper的工作线程中。

但是对于异步查询,您需要一个循环器来确保查询结果可以在 Android 上返回。

因此,要解决您的问题,只需简单地使用同步查询,让它 运行 在事务的工作线程中。如:

RealmResults<UserVehicle> completed = realm.where(UserVehicle.class).equalTo("id", userVehicleID).findAll();