I have been following the course on udacity and have nearly finished lesson 2. The program should be updating the list view with the now parsed data on the JSON
. Except when the following code is added the program crashes:
@Override
protected void onPostExecute(String[] result) {
if (result != null) {
forecastAdapter.clear();
for (String dayForecastStr : result) {
forecastAdapter.add(dayForecastStr);
}
}
}
If that code is not there the program runs as usual and logs the results of the json parse. Others have suggested to
"Make sure you don't redefine the mForecastAdapter in "onCreateView" and only have : mForecastAdapter = new ArrayAdapter"
I'm unsure about how to go ahead of this. I already have mForecastAdapter = new ArrayAdapter<string>
at the head of my class. And the rest of the code on the oncreateview is just loading the array into the listview.
The error log I currently have is:
11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Wed, Nov 5 - Clear - 7/7 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Thu, Nov 6 - Rain - 12/9 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Fri, Nov 7 - Rain - 12/10 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Sat, Nov 8 - Rain - 12/11 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Sun, Nov 9 - Rain - 15/11 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Mon, Nov 10 - Clear - 15/12 11-05 19:29:00.049 20310-20553/com.example.jeremy.sunshine V/FetchWeatherTask﹕ Forecast entry: Tue, Nov 11 - Clear - 15/11 11-05 19:29:00.049 20310-20310/com.example.jeremy.sunshine D/AndroidRuntime﹕ Shutting down VM 11-05 19:29:00.049 20310-20310/com.example.jeremy.sunshine W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415c5db8) 11-05 19:29:00.049 20310-20310/com.example.jeremy.sunshine E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.jeremy.sunshine, PID: 20310 java.lang.NullPointerException at com.example.jeremy.sunshine.ForecastFragment$FetchWeatherTask.onPostExecute(ForecastFragment.java:305) at com.example.jeremy.sunshine.ForecastFragment$FetchWeatherTask.onPostExecute(ForecastFragment.java:108) at android.os.AsyncTask.finish(AsyncTask.java:632) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5146) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612) at dalvik.system.NativeStart.main(Native Method)
My whole class can be seen here:
As this is my first android project I'm baffled to why this won't work. I presume its something I must be overlooking.http://pastebin.com/x3rcqbdS