无法在尚未为天气应用程序调用 Looper.prepare() 的线程中创建处理程序



我正在为一个学校项目制作一个天气应用程序,遇到了一个小问题。我看到很多人问这个问题,我尝试了很多答案,但没有用。我真的是这个东西的新手,所以有可能我没有正确实现这些答案。这是我的代码:

private void getWeatherInformation() {
    compositeDisposable.add(mService.getWeatherByLatLng(String.valueOf(APIKljuc.current_location.getLatitude()),
            String.valueOf(APIKljuc.current_location.getLongitude()),
            APIKljuc.APP_ID,
            "metric")
            .subscribeOn(Schedulers.io())
            .subscribe(new Consumer<WeatherResult>() {
                @Override
                public void accept(WeatherResult weatherResult) throws Exception {
                    //Slika
                    Picasso.get().load(new StringBuilder("https://openweathermap.org/img/w/")
                            .append(weatherResult.getWeather().get(0).getIcon())
                    .append(".png").toString()).into(img_weather);
                    //Ucitavanje informacija
                    txt_city_name.setText(weatherResult.getName());
                    txt_description.setText(new StringBuilder("Weather in")
                    .append(weatherResult.getName()).toString());
                    txt_temperature.setText(new StringBuilder(
                            String.valueOf(weatherResult.getMain().getTemp())).append("°C").toString());
                    txt_date_time.setText(APIKljuc.convertUnixToDate(weatherResult.getDt()));
                    txt_pressure.setText(new StringBuilder(String.valueOf(weatherResult.getMain().getPressure())).append(" hpa").toString());
                    txt_humidity.setText(new StringBuilder(String.valueOf(weatherResult.getMain().getHumidity())).append(" %").toString());
                    txt_sunrise.setText(APIKljuc.convertUnixToHour(weatherResult.getSys().getSunrise()));
                    txt_sundown.setText(APIKljuc.convertUnixToHour(weatherResult.getSys().getSunset()));
                    txt_geo_coords.setText(new StringBuilder("[").append(weatherResult.getCoord().toString()).append("]").toString());
                    txt_wind.setText(new StringBuilder().append(weatherResult.getCoord().toString()).append(" ").toString());
                    //Display panel
                    weather_panel.setVisibility(View.VISIBLE);
                    loading.setVisibility(View.GONE);

                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    Toast.makeText(getActivity(), ""+throwable.getMessage(), Toast.LENGTH_LONG).show();
                }
            })
    );
}

我在网上看到的最推荐的答案是:

  activity.runOnUiThread(new Runnable() {
       public void run() { 
            Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
    }
  });

我用它来替换:新的消费者() {等等.});

我不明白我需要用什么替换"活动",它的红色和我的代码无法编译。

使用它来移动线程

  .observeOn(AndroidSchedulers.mainThread())

我看到你用

.subscribeOn(Schedulers.io())

如果需要像 setText() 这样的操作视图,您需要移动到主线程

.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Cus)

相关内容

最新更新