Search code examples
javaandroidhandlertoastlooper

Can't create handler inside thread that has not called Looper.prepare() for weather app


I am making a weather app for a school project and ran into a little problem. I saw a lot of people asking this question and i tried many answers but it didn't work me. I am really new to this stuff and so its possible i didn't implement those answers correctly. Here is my code :

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();
                }
            })

    );
}

Most recommended answer that ive seen online is :

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

and i used it to replace : new Consumer() {etc..});

I don't understand what i need to replace 'activity' with, its red and my code wont compile..


Solution

  • use this for moving thread

      .observeOn(AndroidSchedulers.mainThread())
    

    i see you use

    .subscribeOn(Schedulers.io())
    

    you need move to mainThread if want manipulate view like setText()

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