无法在尚未为天气应用程序调用 Looper.prepare() 的线程内创建处理程序
Can't create handler inside thread that has not called Looper.prepare() for weather app
我正在为一个学校项目制作一个天气应用程序,运行遇到了一个小问题。我看到很多人问这个问题,我尝试了很多答案,但对我没有用。我对这些东西真的很陌生,所以我可能没有正确实现这些答案。这是我的代码:
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();
}
});
我用它来替换:new Consumer() {etc..});
我不明白我需要用什么替换 'activity',它是红色的,我的代码无法编译..
用它来移动线程
.observeOn(AndroidSchedulers.mainThread())
我看到你用
.subscribeOn(Schedulers.io())
如果想要像 setText() 这样的操作视图,你需要移动到 mainThread
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Cus)
我正在为一个学校项目制作一个天气应用程序,运行遇到了一个小问题。我看到很多人问这个问题,我尝试了很多答案,但对我没有用。我对这些东西真的很陌生,所以我可能没有正确实现这些答案。这是我的代码:
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();
}
});
我用它来替换:new Consumer() {etc..});
我不明白我需要用什么替换 'activity',它是红色的,我的代码无法编译..
用它来移动线程
.observeOn(AndroidSchedulers.mainThread())
我看到你用
.subscribeOn(Schedulers.io())
如果想要像 setText() 这样的操作视图,你需要移动到 mainThread
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Cus)