我正在开发一个Android应用程序,其中我在一个片段活动中有许多片段。我现在有一个新的片段,里面有一个对fragmentpageradapter的调用。但是,这会抛出空指针异常。有可能这样做吗?
在onPostExecute(AsyncTask)中的片段内: -
DailyForecastPageAdapter adapter = new DailyForecastPageAdapter(Integer.parseInt(forecastDaysNum),getActivity().getChildFragmentManager(), forecastWeather);
我面临问题的天气碎片。
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;
}
私有类JSONWeatherTask扩展了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() + "°");
*/
}
}
私有类JSONForecastWeatherTask扩展了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)
任何人都可以帮我解决/理解为什么我会获得NPE?我真的只需要它作为片段。
答案 0 :(得分:1)
我找到了解决方案。问题是由于我在引用所有textViews时使用了getActivity(),而getChildFragmentManager()足以创建FragmentPagerAdapter的对象。
而不是getActivity(),我使用了我的视图,即rootView(在我的情况下),这解决了问题。 :)
答案 1 :(得分:0)
将以下代码移至:
JSONWeatherTask task = new JSONWeatherTask();
task.execute(new String[]{city,lang});
JSONForecastWeatherTask task1 = new JSONForecastWeatherTask();
task1.execute(new String[]{city,lang, forecastDaysNum});
到onActivityCreated
或onAtach