I am developing an android application in which I have many fragments inside one fragment activity. I have a new fragment now inside which there is a call to a fragmentpageradapter. This however throws a null pointer exception. Is it possible to do this?
Inside my fragment in an onPostExecute (AsyncTask) :-
DailyForecastPageAdapter adapter = new DailyForecastPageAdapter(Integer.parseInt(forecastDaysNum),getActivity().getChildFragmentManager(), forecastWeather);
Weather Fragment where I am facing the problem.
public class WeatherFrag extends Fragment{
private TextView cityText;
private TextView condDescr;
private TextView temp;
//private TextView press;
//private TextView windSpeed;
//private TextView windDeg;
private TextView unitTemp;
//private TextView hum;
private ImageView imgView;
private static String forecastDaysNum = "5";
private ViewPager pager;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.weather_main, container, false);
String city = "London, UK";
String lang = "en";
cityText = (TextView) getActivity().findViewById(R.id.cityText);
temp = (TextView) getActivity().findViewById(R.id.temp);
unitTemp = (TextView) getActivity().findViewById(R.id.unittemp);
//unitTemp.setText("°C");
condDescr = (TextView) getActivity().findViewById(R.id.skydesc);
pager = (ViewPager) getActivity().findViewById(R.id.pager);
imgView = (ImageView) getActivity().findViewById(R.id.condIcon);
JSONWeatherTask task = new JSONWeatherTask();
task.execute(new String[]{city,lang});
JSONForecastWeatherTask task1 = new JSONForecastWeatherTask();
task1.execute(new String[]{city,lang, forecastDaysNum});
return rootView;
}
private class JSONWeatherTask extends AsyncTask {
@Override
protected Weather doInBackground(String... params) {
Weather weather = new Weather();
String data = ( (new WeatherHttpClient()).getWeatherData(params[0], params[1]));
try {
weather = JSONWeatherParser.getWeather(data);
System.out.println("Weather ["+weather+"]");
// Let's retrieve the icon
weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));
} catch (JSONException e) {
e.printStackTrace();
}
return weather;
}
@Override
protected void onPostExecute(Weather weather) {
super.onPostExecute(weather);
Log.d("Err","Inside Post Exe");
if (weather.iconData != null && weather.iconData.length > 0) {
Log.d("Err","Inside Post Exe-if");
Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0, weather.iconData.length);
imgView.setImageBitmap(img);
}
Log.d("Err","Inside On Post Exe-Out");
cityText.setText(weather.location.getCity() + "," + weather.location.getCountry());
temp.setText("" + Math.round((weather.temperature.getTemp() - 275.15)));
condDescr.setText(weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")");
/*
temp.setText("" + Math.round((weather.temperature.getTemp() - 275.15)) + "°C");
hum.setText("" + weather.currentCondition.getHumidity() + "%");
press.setText("" + weather.currentCondition.getPressure() + " hPa");
windSpeed.setText("" + weather.wind.getSpeed() + " mps");
windDeg.setText("" + weather.wind.getDeg() + "°");
*/
}
}
private class JSONForecastWeatherTask extends AsyncTask {
@Override
protected WeatherForecast doInBackground(String... params) {
String data = ( (new WeatherHttpClient()).getForecastWeatherData(params[0], params[1], params[2]));
WeatherForecast forecast = new WeatherForecast();
try {
forecast = JSONWeatherParser.getForecastWeather(data);
System.out.println("Weather ["+forecast+"]");
//weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));
} catch (JSONException e) {
e.printStackTrace();
}
return forecast;
}
@Override
protected void onPostExecute(WeatherForecast forecastWeather) {
super.onPostExecute(forecastWeather);
Log.d("Err","Reached onpostexe");
DailyForecastPageAdapter adapter = new DailyForecastPageAdapter(Integer.parseInt(forecastDaysNum),getChildFragmentManager(), forecastWeather);
Log.d("Err","error before");
pager.setAdapter(adapter);
}
}
}
LogCat
02-20 20:25:51.847: E/AndroidRuntime(5241): FATAL EXCEPTION: main
02-20 20:25:51.847: E/AndroidRuntime(5241): Process: com.akisoft.slidingmenu, PID: 5241
02-20 20:25:51.847: E/AndroidRuntime(5241): java.lang.NullPointerException
02-20 20:25:51.847: E/AndroidRuntime(5241): at com.akisoft.weather.main.WeatherFrag$JSONWeatherTask.onPostExecute(WeatherFrag.java:94)
02-20 20:25:51.847: E/AndroidRuntime(5241): at com.akisoft.weather.main.WeatherFrag$JSONWeatherTask.onPostExecute(WeatherFrag.java:1)
02-20 20:25:51.847: E/AndroidRuntime(5241): at android.os.AsyncTask.finish(AsyncTask.java:632)
02-20 20:25:51.847: E/AndroidRuntime(5241): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-20 20:25:51.847: E/AndroidRuntime(5241): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
02-20 20:25:51.847: E/AndroidRuntime(5241): at android.os.Handler.dispatchMessage(Handler.java:102)
02-20 20:25:51.847: E/AndroidRuntime(5241): at android.os.Looper.loop(Looper.java:136)
02-20 20:25:51.847: E/AndroidRuntime(5241): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-20 20:25:51.847: E/AndroidRuntime(5241): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 20:25:51.847: E/AndroidRuntime(5241): at java.lang.reflect.Method.invoke(Method.java:515)
02-20 20:25:51.847: E/AndroidRuntime(5241): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-20 20:25:51.847: E/AndroidRuntime(5241): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-20 20:25:51.847: E/AndroidRuntime(5241): at dalvik.system.NativeStart.main(Native Method)
Can anyone help me solve/understand why am I getting a NPE? I really need it as a fragment only.