Java Can';t在未调用weather应用程序的Looper.prepare()的线程内创建处理程序
我正在为一个学校项目制作天气应用程序,遇到了一个小问题。我看到很多人问这个问题,我尝试了很多答案,但都不管用。我对这些东西真的很陌生,所以我可能没有正确地实现这些答案。这是我的密码:Java Can';t在未调用weather应用程序的Looper.prepare()的线程内创建处理程序,java,android,handler,toast,looper,Java,Android,Handler,Toast,Looper,我正在为一个学校项目制作天气应用程序,遇到了一个小问题。我看到很多人问这个问题,我尝试了很多答案,但都不管用。我对这些东西真的很陌生,所以我可能没有正确地实现这些答案。这是我的密码: private void getWeatherInformation() { compositeDisposable.add(mService.getWeatherByLatLng(String.valueOf(APIKljuc.current_location.getLatitude()),
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();
}
})
);
}
我用它来替换:newconsumer(){etc..})
我不明白我需要用什么来替换“活动”,它是红色的,我的代码无法编译。用它来移动线程
.observeOn(AndroidSchedulers.mainThread())
我看你用
.subscribeOn(Schedulers.io())
如果想要操纵像setText()这样的视图,您需要移动到mainThread
非常感谢,这很有效,我只添加了.observeOn(AndroidSchedulers.mainThread())和left.subscribeOn(Schedulers.io())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Cus)