我正在尝试创建一个使用 Jack Wharton 的 ViewPager 库(此处)的应用程序,每个页面仅使用图像(类似于 Prixing(此处)主屏幕)。一切正常,除了片段中的 saveInstance。
在 Jack Wharton 的示例中,他将文本存储在一个名为 mContent 的 String 变量中,并在 onCreate 中恢复它,但在我的情况下,我该怎么办?保存/恢复位图?!任何客观的答案将不胜感激!
我对这个使用应用程序的 Fragment 很陌生,因为我看到的每个示例都只介绍了基础知识,而对于更复杂的示例则变得越来越困难。
PS。如果知道有用的话,我正在使用 CirclePageIndicator。
当前的片段代码在这里:
public final class SpecialOfferFragment extends Fragment {
private int imageResourceId;
public static SpecialOfferFragment newInstance(int i) {
//probably I'll use a bitmap(downloaded) as parameter instead of using static images
SpecialOfferFragment fragment = new SpecialOfferFragment();
fragment.imageResourceId = i;
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// if ((savedInstanceState != null) { }
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ImageView image = new ImageView(getActivity());
image.setImageResource(imageResourceId);
image.setScaleType(ScaleType.FIT_XY);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(image);
return layout;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//smth to save here..
}
}
在应用程序的当前状态下,我收到以下异常:
04-12 07:28:17.760: E/AndroidRuntime(31903): FATAL EXCEPTION: main
04-12 07:28:17.760: E/AndroidRuntime(31903): java.lang.NullPointerException
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1576)
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1617)
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:481)
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.app.Activity.performSaveInstanceState(Activity.java:1113)
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1188)
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:2804)
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:2862)
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.app.ActivityThread.access$900(ActivityThread.java:127)
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1175)
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.os.Looper.loop(Looper.java:137)
04-12 07:28:17.760: E/AndroidRuntime(31903): at android.app.ActivityThread.main(ActivityThread.java:4511)
04-12 07:28:17.760: E/AndroidRuntime(31903): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 07:28:17.760: E/AndroidRuntime(31903): at java.lang.reflect.Method.invoke(Method.java:511)
04-12 07:28:17.760: E/AndroidRuntime(31903): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
04-12 07:28:17.760: E/AndroidRuntime(31903): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
04-12 07:28:17.760: E/AndroidRuntime(31903): at dalvik.system.NativeStart.main(Native Method)
每当我暂停/停止包含此片段的活动时,都会发生这种情况,例如按下主页按钮。
**
编辑代码:
**
public class MainMenu extends FragmentActivity {
//private List<CategoriesHolder> categoriesList = new ArrayList<CategoriesHolder>();
//private CategoriesAdapter categoriesAdapter = null;
//private GridView gv_mainmenu_categories;
SpecialOfferFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainmenu);
linkUI();
setAction();
// just for testing
String[] imagesUrls = null;
mAdapter = new SpecialOfferFragmentAdapter(getSupportFragmentManager(),
this, imagesUrls);
//categoriesAdapter = new CategoriesAdapter(this, categoriesList);
// used for Categories GridView
//gv_mainmenu_categories.setAdapter(categoriesAdapter);
// used for ViewPager
mPager.setAdapter(mAdapter);
mIndicator.setViewPager(mPager);
}
private void linkUI() {
mPager = (ViewPager) findViewById(R.id.vp_mainmenu_special_offers);
mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
//gv_mainmenu_categories = (GridView) findViewById(R.id.gv_mainmenu_categories);
}
private void setAction() {
}
}
public class SpecialOfferFragmentAdapter extends FragmentPagerAdapter implements
IconPagerAdapter {
private int[] mCarouselImages = new int[] { R.drawable.pic1,
R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5
};
private String[] imagesUrls;
private Context context;
public static final int[] ICONS = new int[] {
R.drawable.perm_group_calendar, R.drawable.perm_group_camera,
R.drawable.perm_group_device_alarms, R.drawable.perm_group_location };
private int mCount = mCarouselImages.length;
public SpecialOfferFragmentAdapter(FragmentManager fm, Context context,
String[] imagesUrls) {
super(fm);
this.context = context;
this.imagesUrls = imagesUrls;
}
@Override
public Fragment getItem(int position) {
return SpecialOfferFragment.newInstance(mCarouselImages[position]);
}
@Override
public int getCount() {
return mCount;
}
@Override
public int getIconResId(int index) {
return ICONS[index % ICONS.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}