我正在使用ViewPager和片段创建一个演练活动,我已经能够成功创建演练,但问题是,我只能向前滑动,如果我尝试刷回应用程序崩溃,我试过在另一个前后都完美运行的测试应用程序中复制它,虽然那个使用TextView,而这个使用ImageView。
我需要帮助弄清楚它为什么在向后滑动时崩溃。
public class WalkThroughActivity extends AppCompatActivity {
ViewPager mPager;
LinearLayout mLinearLayout;
CustomPagerAdapter mAdapter;
MyPageIndicator mIndicator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_walk_through);
mPager = (ViewPager) findViewById(R.id.pager);
mLinearLayout = (LinearLayout) findViewById(R.id.pagesContainer);
List < Fragment > fragments = new ArrayList < >();
fragments.add(ContentFragment.newInstance(R.drawable.walkthrough_card_1));
fragments.add(ContentFragment.newInstance(R.drawable.walkthrough_card_2));
fragments.add(ContentFragment.newInstance(R.drawable.walkthrough_card_3));
mAdapter = new CustomPagerAdapter(getSupportFragmentManager(), fragments);
mPager.setAdapter(mAdapter);
mIndicator = new MyPageIndicator(this, mLinearLayout, mPager, R.drawable.indicator_circle);
mIndicator.setPageCount(fragments.size());
mIndicator.show();
}
@Override
protected void onDestroy() {
super.onDestroy();
mIndicator.cleanup();
}
static class CustomPagerAdapter extends FragmentStatePagerAdapter {
List < Fragment > mFrags = new ArrayList < >();
public CustomPagerAdapter(FragmentManager fm, List < Fragment > frags) {
super(fm);
mFrags = frags;
}
@Override
public Fragment getItem(int position) {
int index = position % mFrags.size();
return mFrags.get(index);
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
}
}
ContentFragment
public class ContentFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private int mParam1;
public ContentFragment() {
// Required empty public constructor
}
public static ContentFragment newInstance(int param1) {
ContentFragment fragment = new ContentFragment();
Bundle args = new Bundle();
args.putInt(ARG_PARAM1, param1);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getInt(ARG_PARAM1);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.indicator_fragment, container, false);;
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
imageView.setImageResource(mParam1);
return view;
}
public int getmParam1() {
return mParam1;
}
}
错误记录
FATAL EXCEPTION: main
Process: com.simpumind.shoopa, PID: 8207
java.lang.IllegalStateException: Fragment already active
at android.support.v4.app.Fragment.setInitialSavedState(Fragment.java:576)
at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:114)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1034)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1216)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1116)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:273)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
答案 0 :(得分:0)
我认为违规的代码行是
@Override
public Fragment getItem(int position) {
int index = position % mFrags.size();
return mFrags.get(index);
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
我不确定你想要实现的目标,但这段代码非常糟糕。它使ViewPager
相信你拥有的碎片比你实际拥有的碎片多得多,并且多次重复使用相同的碎片。假设mFrags.size()
为2而ViewPager
请求#0,#1和#2 - 您将返回片段#0两次,ViewPager
可能会尝试传递相同的片段生命周期两次。这种片段的重复使用很可能是您看到的异常的来源。
顺便说一下,直截了当有什么不对?
@Override
public Fragment getItem(int position) {
return mFrags.get(position);
}
@Override
public int getCount() {
return mFrags.size();
}