因此,當用戶在每個頁面上滑動時,我想要在最後啓動一個意圖;展示主要活動。現在,我將頁數設置爲4,以便當用戶滑動到第4頁時,用戶會被髮送到我的主要活動。但是我的應用程序崩潰,試圖在PageViewer結束時啓動意圖。應用程序崩潰
片段活動
public class ScreenSlideActivity extends FragmentActivity {
private static final int NUM_PAGES = 4;
ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
public void onBackPressed() {
int pos = mPager.getCurrentItem();
if (pos == 0) {
super.onBackPressed();
} else mPager.setCurrentItem(pos - 1);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new adapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
private class adapter extends FragmentStatePagerAdapter {
public adapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 4) {
Intent intent = new Intent(getApplication(), MainActivity.class);
startActivity(intent);
}
return ScreenSlidePageFragment.initialize(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
片段
public class ScreenSlidePageFragment extends Fragment {
int position;
String[] text = {"Be more productive", "Choose your own work and rest times", "Login to access full features"};
int[] images = {R.drawable.productivity, R.drawable.workingguy, R.drawable.logincmon};
public static ScreenSlidePageFragment initialize(int position) {
ScreenSlidePageFragment page = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt("POSITION", position);
page.setArguments(args);
return page;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt("POSITION", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
TextView tv = (TextView) v.findViewById(R.id.MainText);
ImageView image = (ImageView) v.findViewById(R.id.MainImage);
tv.setText(text[position]);
image.setImageResource(images[position]);
return v;
}
}
什麼是這次崩潰的堆棧跟蹤? –
'if(position == 4){'這需要3,從0..3開始給你4頁 –
我認爲解決這個問題的方法是調用某種outofbounds監聽器。但是,我無法找到一個 –