Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android开发:致命异常:AsyncTask#1_Android_Json_Api_Android Asynctask_Futuretask - Fatal编程技术网

Android开发:致命异常:AsyncTask#1

Android开发:致命异常:AsyncTask#1,android,json,api,android-asynctask,futuretask,Android,Json,Api,Android Asynctask,Futuretask,我一直在努力学习一些关于Android开发的知识。在几个“Hello World”和简单的应用程序之后,我决定制作一些我可以实际使用的东西:一个简单的天气应用程序。我找到了一个不错的,所以我一直在跟踪 我一直在通读它,试图理解它,并使它适应我自己的项目,而不仅仅是复制和粘贴它 当我尝试在手机上运行它时,在Android显示器上出现以下错误: E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.anatis.ueder,

我一直在努力学习一些关于Android开发的知识。在几个“Hello World”和简单的应用程序之后,我决定制作一些我可以实际使用的东西:一个简单的天气应用程序。我找到了一个不错的,所以我一直在跟踪

我一直在通读它,试图理解它,并使它适应我自己的项目,而不仅仅是复制和粘贴它

当我尝试在手机上运行它时,在Android显示器上出现以下错误:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.anatis.ueder, PID: 29363
    java.lang.RuntimeException: An error occured while executing doInBackground()
      at android.os.AsyncTask$3.done(AsyncTask.java:304)
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
      at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
      at java.lang.Thread.run(Thread.java:818)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
      at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
      at org.json.JSONTokener.nextValue(JSONTokener.java:94)
      at org.json.JSONObject.<init>(JSONObject.java:156)
      at org.json.JSONObject.<init>(JSONObject.java:173)
      at com.anatis.ueder.JSONWeatherParser.getWeather(JSONWeatherParser.java:17)
      at com.anatis.ueder.MainActivity$JSONWeatherTask.doInBackground(MainActivity.java:63)
      at com.anatis.ueder.MainActivity$JSONWeatherTask.doInBackground(MainActivity.java:55)
      at android.os.AsyncTask$2.call(AsyncTask.java:292)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
      at java.lang.Thread.run(Thread.java:818)
下面是来自JSONWeatherParser.java的getWeather()

public static Weather getWeather(String data) throws JSONException {
    Weather weather = new Weather();

    // We create out JSONObject from the data
    JSONObject jObj = new JSONObject(data);

    // We start extracting the info
    Location loc = new Location();

    JSONObject coordObj = getObject("coord", jObj);
    loc.setLatitude(getFloat("lat", coordObj));
    loc.setLongitude(getFloat("lon", coordObj));

    JSONObject sysObj = getObject("sys", jObj);
    loc.setCountry(getString("country", sysObj));
    loc.setSunrise(getInt("sunrise", sysObj));
    loc.setSunset(getInt("sunset", sysObj));
    loc.setCity(getString("name", jObj));
    weather.location = loc;

    // We get weather info (This is an array)
    JSONArray jArr = jObj.getJSONArray("weather");

    // We use only the first value
    JSONObject JSONWeather = jArr.getJSONObject(0);
    weather.currentCondition.setWeatherId(getInt("id", JSONWeather));
    weather.currentCondition.setDescr(getString("description", JSONWeather));
    weather.currentCondition.setCondition(getString("main", JSONWeather));
    weather.currentCondition.setIcon(getString("icon", JSONWeather));

    JSONObject mainObj = getObject("main", jObj);
    weather.currentCondition.setHumidity(getInt("humidity", mainObj));
    weather.currentCondition.setPressure(getInt("pressure", mainObj));
    weather.temperature.setMaxTemp(getFloat("temp_max", mainObj));
    weather.temperature.setMinTemp(getFloat("temp_min", mainObj));
    weather.temperature.setTemp(getFloat("temp", mainObj));

    // Wind
    JSONObject wObj = getObject("wind", jObj);
    weather.wind.setSpeed(getFloat("speed", wObj));
    weather.wind.setDeg(getFloat("deg", wObj));

    // Clouds
    JSONObject cObj = getObject("clouds", jObj);
    weather.clouds.setPerc(getInt("all", cObj));

    // We download the icon to show


    return weather;
}
你知道发生了什么事吗?我该怎么解决

如果您需要查看任何其他代码,请告诉我

我真的很感激我能得到的所有帮助(我希望在圣诞节前把这个应用程序运行起来,这样我就可以把这个应用程序作为圣诞礼物送给我的父亲……;)


谢谢你

您的错误是java.lang.NullPointerException:尝试对空对象引用调用虚拟方法“int java.lang.String.length()”,因此请更新您的代码什么是JSONWeatherParser.java:17???Aashvi,我应该如何更新代码?Stefan,JSONWeatherParser.java是我从教程中复制的一个类。你可以看到,我刚刚更新了包名以适合我的项目。你应该尝试在字符串上放置一个日志,你尝试提取的长度是多少,然后看看它返回了什么。。通过log catman,您的代码不完整,从哪里获取字符串的长度?错误为java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“int java.lang.string.length()”,因此请更新您的代码什么是JSONWeatherParser.java:17???Aashvi,我应该如何更新代码?Stefan,JSONWeatherParser.java是我从教程中复制的一个类。你可以看到,我刚刚更新了包名以适合我的项目。你应该尝试在字符串上放置一个日志,你尝试提取的长度是多少,然后看看它返回了什么。。通过log catman,您的代码不完整,从哪里获取字符串的长度?
public static Weather getWeather(String data) throws JSONException {
    Weather weather = new Weather();

    // We create out JSONObject from the data
    JSONObject jObj = new JSONObject(data);

    // We start extracting the info
    Location loc = new Location();

    JSONObject coordObj = getObject("coord", jObj);
    loc.setLatitude(getFloat("lat", coordObj));
    loc.setLongitude(getFloat("lon", coordObj));

    JSONObject sysObj = getObject("sys", jObj);
    loc.setCountry(getString("country", sysObj));
    loc.setSunrise(getInt("sunrise", sysObj));
    loc.setSunset(getInt("sunset", sysObj));
    loc.setCity(getString("name", jObj));
    weather.location = loc;

    // We get weather info (This is an array)
    JSONArray jArr = jObj.getJSONArray("weather");

    // We use only the first value
    JSONObject JSONWeather = jArr.getJSONObject(0);
    weather.currentCondition.setWeatherId(getInt("id", JSONWeather));
    weather.currentCondition.setDescr(getString("description", JSONWeather));
    weather.currentCondition.setCondition(getString("main", JSONWeather));
    weather.currentCondition.setIcon(getString("icon", JSONWeather));

    JSONObject mainObj = getObject("main", jObj);
    weather.currentCondition.setHumidity(getInt("humidity", mainObj));
    weather.currentCondition.setPressure(getInt("pressure", mainObj));
    weather.temperature.setMaxTemp(getFloat("temp_max", mainObj));
    weather.temperature.setMinTemp(getFloat("temp_min", mainObj));
    weather.temperature.setTemp(getFloat("temp", mainObj));

    // Wind
    JSONObject wObj = getObject("wind", jObj);
    weather.wind.setSpeed(getFloat("speed", wObj));
    weather.wind.setDeg(getFloat("deg", wObj));

    // Clouds
    JSONObject cObj = getObject("clouds", jObj);
    weather.clouds.setPerc(getInt("all", cObj));

    // We download the icon to show


    return weather;
}