尝试找出在我的应用中投放NullPointerException
的内容。有人能帮助我吗?
错误行
listener.onWeatherResponse(result);
主要代码:
public class YahooClient {
public static List<CityResult> getCityList(String cityName) {
List<CityResult> result = new ArrayList<CityResult>();
HttpURLConnection yahooHttpConn = null;
try {
String query =makeQueryCityURL(cityName);
Log.d("Swa", "URL [" + query + "]");
yahooHttpConn= (HttpURLConnection) (new URL(query)).openConnection();
yahooHttpConn.connect();
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(new InputStreamReader(yahooHttpConn.getInputStream()));
Log.d("Swa", "XML Parser ok");
int event = parser.getEventType();
CityResult cty = null;
String tagName = null;
String currentTag = null;
// We start parsing the XML
while (event != XmlPullParser.END_DOCUMENT) {
tagName = parser.getName();
if (event == XmlPullParser.START_TAG) {
if (tagName.equals("place")) {
// place Tag Found so we create a new CityResult
cty = new CityResult();
Log.d("Swa", "New City found");
}
currentTag = tagName;
Log.d("Swa", "Tag ["+tagName+"]");
}
else if (event == XmlPullParser.TEXT) {
// We found some text. let's see the tagName to know the tag related to the text
if ("woeid".equals(currentTag))
cty.setWoeid(parser.getText());
else if ("name".equals(currentTag))
cty.setCityName(parser.getText());
else if ("country".equals(currentTag))
cty.setCountry(parser.getText());
// We don't want to analyze other tag at the moment
}
else if (event == XmlPullParser.END_TAG) {
if ("place".equals(tagName))
result.add(cty);
}
event = parser.next();
}
}
catch(Throwable t) {
t.printStackTrace();
Log.e("Error in getCityList", t.getMessage());
}
finally {
try {
yahooHttpConn.disconnect();
}
catch(Throwable ignore) {}
}
return result;
}
public static void getWeather(String woeid, String unit, RequestQueue rq, final WeatherClientListener listener) {
String url2Call = makeWeatherURL(woeid, unit);
Log.d("SwA", "Weather URL ["+url2Call+"]");
final Weather result = new Weather();
StringRequest req = new StringRequest(Request.Method.GET, url2Call, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
parseResponse(s, result);
listener.onWeatherResponse(result);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
rq.add(req);
}
private static Weather parseResponse (String resp, Weather result) {
Log.d("SwA", "Response ["+resp+"]");
try {
XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
parser.setInput(new StringReader(resp));
String tagName = null;
String currentTag = null;
int event = parser.getEventType();
boolean isFirstDayForecast = true;
while (event != XmlPullParser.END_DOCUMENT) {
tagName = parser.getName();
if (event == XmlPullParser.START_TAG) {
if (tagName.equals("yweather:wind")) {
// Log.d("SwA", "Tag [Wind]");
result.wind.chill = Integer.parseInt(parser.getAttributeValue(null, "chill"));
result.wind.direction = Integer.parseInt(parser.getAttributeValue(null, "direction"));
result.wind.speed = (int) Float.parseFloat(parser.getAttributeValue(null, "speed"));
}
else if (tagName.equals("yweather:atmosphere")) {
// Log.d("SwA", "Tag [Atmos]");
result.atmosphere.humidity = Integer.parseInt(parser.getAttributeValue(null, "humidity"));
result.atmosphere.visibility = Float.parseFloat(parser.getAttributeValue(null, "visibility"));
result.atmosphere.pressure = Float.parseFloat(parser.getAttributeValue(null, "pressure"));
result.atmosphere.rising = Integer.parseInt(parser.getAttributeValue(null, "rising"));
}
else if (tagName.equals("yweather:forecast")) {
// Log.d("SwA", "Tag [Fore]");
if (isFirstDayForecast) {
result.forecast.code = Integer.parseInt(parser.getAttributeValue(null, "code"));
result.forecast.tempMin = Integer.parseInt(parser.getAttributeValue(null, "low"));
result.forecast.tempMax = Integer.parseInt(parser.getAttributeValue(null, "high"));
isFirstDayForecast = false;
}
}
else if (tagName.equals("yweather:condition")) {
// Log.d("SwA", "Tag [Condition]");
result.condition.code = Integer.parseInt(parser.getAttributeValue(null, "code"));
result.condition.description = parser.getAttributeValue(null, "text");
result.condition.temp = Integer.parseInt(parser.getAttributeValue(null, "temp"));
result.condition.date = parser.getAttributeValue(null, "date");
}
else if (tagName.equals("yweather:units")) {
// Log.d("SwA", "Tag [units]");
result.units.temperature = "°" + parser.getAttributeValue(null, "temperature");
result.units.pressure = parser.getAttributeValue(null, "pressure");
result.units.distance = parser.getAttributeValue(null, "distance");
result.units.speed = parser.getAttributeValue(null, "speed");
}
else if (tagName.equals("yweather:location")) {
result.location.name = parser.getAttributeValue(null, "city");
result.location.region = parser.getAttributeValue(null, "region");
result.location.country = parser.getAttributeValue(null, "country");
}
else if (tagName.equals("image"))
currentTag = "image";
else if (tagName.equals("url")) {
if (currentTag == null) {
result.imageUrl = parser.getAttributeValue(null, "src");
}
}
else if (tagName.equals("lastBuildDate")) {
currentTag="update";
}
else if (tagName.equals("yweather:astronomy")) {
result.astronomy.sunRise = parser.getAttributeValue(null, "sunrise");
result.astronomy.sunSet = parser.getAttributeValue(null, "sunset");
}
}
else if (event == XmlPullParser.END_TAG) {
if ("image".equals(currentTag)) {
currentTag = null;
}
}
else if (event == XmlPullParser.TEXT) {
if ("update".equals(currentTag))
result.lastUpdate = parser.getText();
}
event = parser.next();
}
}
catch(Throwable t) {
t.printStackTrace();
}
return result;
}
private static String makeQueryCityURL(String cityName) {
// We remove spaces in cityName
cityName = cityName.replaceAll(" ", "%20");
return YAHOO_GEO_URL + "/places.q(" + cityName + "%2A);count=" + Config.MAX_CITY_RESULT + "?appid=" + APPID;
}
private static String makeWeatherURL(String woeid, String unit) {
return YAHOO_WEATHER_URL + "?w=" + woeid + "&u=" + unit;
}
/* Pubblic listener interface */
public static interface WeatherClientListener {
public void onWeatherResponse(Weather weather);
}
}
我的日志文件
E/AndroidRuntime(14436): java.lang.NullPointerException
01 E / AndroidRuntime(14436):at com.ocs76584.cityinfo.Activity.WeatherActivity $ 2.onWeatherResponse(WeatherActivity.java:135) 01-10 18:20:01.346:E / AndroidRuntime(14436):at com.ocs76584.cityinfo.Provider.YahooClient $ 1.onResponse(YahooClient.java:106) 01-10 18:20:01.346:E / AndroidRuntime(14436):at com.ocs76584.cityinfo.Provider.YahooClient $ 1.onResponse(YahooClient.java:1) 01-10 18:20:01.346:E / AndroidRuntime(14436):at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 01-10 18:20:01.346:E / AndroidRuntime(14436):at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 01-10 18:20:01.346:E / AndroidRuntime(14436):at com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 01-10 18:20:01.346:E / AndroidRuntime(14436):在android.os.Handler.handleCallback(Handler.java:615) 01-10 18:20:01.346:E / AndroidRuntime(14436):在android.os.Handler.dispatchMessage(Handler.java:92) 01-10 18:20:01.346:E / AndroidRuntime(14436):在android.os.Looper.loop(Looper.java:137) 01-10 18:20:01.346:E / AndroidRuntime(14436):在android.app.ActivityThread.main(ActivityThread.java:4895) 01-10 18:20:01.346:E / AndroidRuntime(14436):at java.lang.reflect.Method.invokeNative(Native Method) 01-10 18:20:01.346:E / AndroidRuntime(14436):at java.lang.reflect.Method.invoke(Method.java:511) 01-10 18:20:01.346:E / AndroidRuntime(14436):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:994) 01-10 18:20:01.346:E / AndroidRuntime(14436):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 01-10 18:20:01.346:E / AndroidRuntime(14436):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
某处,而不在您已展示的代码中,您正在使用getWeather(String, String, RequestQueue, Listener)
null
的{{1}}来调用Listener
。