我正在创建一个天气应用程序并且异步任务不起作用并导致应用程序崩溃。
这是logcat
07-19 14:27:06.812 23884-23884/com.oxinc.android.drate E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.oxinc.android.drate, PID: 23884
java.lang.NullPointerException: Attempt to invoke virtual method
'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)' on a null object reference
at com.oxinc.android.drate.MainActivity.serviceSuccess(MainActivity.java:219)
at com.oxinc.android.drate.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:94)
at com.oxinc.android.drate.service.YahooWeatherService$1.onPostExecute(YahooWeatherService.java:36)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5456)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
我的异步任务
package com.oxinc.android.drate.service;
import android.net.Uri;
import android.os.AsyncTask;
import com.oxinc.android.drate.data.Channel;
import com.oxinc.android.drate.listener.WeatherServiceListener;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
public class YahooWeatherService {
private WeatherServiceListener listener;
private Exception error;
private String temperatureUnit = "C";
public YahooWeatherService(WeatherServiceListener listener) {
this.listener = listener;
}
public String getTemperatureUnit() {
return temperatureUnit;
}
public void setTemperatureUnit(String temperatureUnit) {
this.temperatureUnit = temperatureUnit;
}
public void refreshWeather(String location) {
new AsyncTask<String, Void, Channel>() {
@Override
protected Channel doInBackground(String[] locations) {
String location = locations[0];
Channel channel = new Channel();
String unit = getTemperatureUnit().equalsIgnoreCase("f") ? "f" : "c";
String YQL = String.format("select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=\"%s\") and u='" + unit + "'", location);
String endpoint = String.format("https://query.yahooapis.com/v1/public/yql?q=%s&format=json", Uri.encode(YQL));
try {
URL url = new URL(endpoint);
URLConnection connection = url.openConnection();
connection.setUseCaches(false);
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
JSONObject data = new JSONObject(result.toString());
JSONObject queryResults = data.optJSONObject("query");
int count = queryResults.optInt("count");
if (count == 0) {
error = new LocationWeatherException("No weather information found for " + location);
return null;
}
JSONObject channelJSON = queryResults.optJSONObject("results").optJSONObject("channel");
channel.populate(channelJSON);
return channel;
} catch (Exception e) {
error = e;
}
return null;
}
@Override
protected void onPostExecute(Channel channel) {
if (channel == null && error != null) {
listener.serviceFailure(error);
} else {
listener.serviceSuccess(channel);
}
}
}.execute(location);
}
public class LocationWeatherException extends Exception {
public LocationWeatherException(String detailMessage) {
super(detailMessage);
}
}
}
提前感谢您的帮助。这是我过去一天的一个问题。我真的很难解决它。这就是我来这里的原因!