我在我的应用程序中使用viewPager和操作栏选项卡,每个片段都有自己的用于UI的片段xml文件,但我有两个问题。第一个问题是,如果我创建3个片段然后当应用程序被拉第一个片段是好的时候我滑到第二个 - 第二个是好然后我滑到第三个并且先返回tu并且第一个片段被损坏 - 具有默认值(所有数据都丢失了但是我不知道为什么)第二个最大的问题是当我创建第三个片段并且我想在他里面初始化项目时我得到空指针因为(我认为第三个片段没有创建或者可能不好)。 PlaceholderFragment3()
这是我的代码>
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new PlaceholderFragment().newInstance(0);
case 1:
return new PlaceholderFragment2().newInstance(0);
case 2:
return new PlaceholderFragment3().newInstance(0);
}
return null;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1);
case 1:
return getString(R.string.title_section2);
case 2:
return getNameOfDayOfWeek(DAY_AFTER_TOMORROW);
}
return null;
}
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
MainActivity.iMainActivity.dateToday=(TextView) rootView.findViewById(R.id.tvDateOfDay);
return rootView;
}
}
创建了另一个片段,就像PlaceholderFragment名称更改为PlaceholderFragment2和3
public static MainActivity iMainActivity; // instance of MainActivity
public class JSONWeatherTaskDay3 extends AsyncTask<String, Void, Weather> {
@Override
protected Weather doInBackground(String... params) {
Weather weatherDay3 = new Weather();
String data = ((new WeatherHttpClient()).getWeatherData(params[0]));
try {
weatherDay3 = JSONWeatherParser.getWeather(data,2);
} catch (JSONException e) {
e.printStackTrace();
}
return weatherDay3;
}
@Override
protected void onPostExecute(Weather weather) {
MainActivity.iMainActivity.cityNameDay3.setText(weather.location.getCity());
}
....
}
JSONWeatherTaskDay3是第三个片段的任务,我得到异常,片段1和2有另一个TASK类
异常&GT;
java.lang.NullPointerException
at com.pfittech.andweather.MainActivity$JSONWeatherTaskDay3.onPostExecute(MainActivity.java:636)
at com.pfittech.andweather.MainActivity$JSONWeatherTaskDay3.onPostExecute(MainActivity.java:618)
at android.os.AsyncTask.finish(AsyncTask.java:602)
at android.os.AsyncTask.access$600(AsyncTask.java:156)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4448)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
at dalvik.system.NativeStart.main(Native Method)