我在我的应用程序中使用ViewPager和主要活动定义它。在的onCreate
方法我加载从共享preferences页一些号码,然后把它传递给PagerAdapter:
I'm using ViewPager in my app and define it in the main Activity. Inside onCreate
method I load some number of pages from SharedPreferences and then pass it to PagerAdapter:
@Override
public int getCount() {
return numberOfPages;
}
现在的问题是,如果我会改变preferences这个数字(或其他活动),以其他一些不太那么网页的索引我看之前,我的应用程序崩溃,因为该指数是出界,当我回到活动与此ViewPager。它可以简单地通过改变活性ViewPager的页是固定的。有没有办法做到这一点?
The problem is that if I would change this number in Preferences (or another Activity) to some other less then page index I viewed before, my app crashes because this index is out of bounds when I return to the activity with this ViewPager. It can be fixed simply by changing active ViewPager's page. Is there any way to do it?
我不知道我完全理解这个问题,但是从你的问题的标题,我猜你要找的是什么 pager.setCurrentItem(NUM)
。这可以让你在编程切换到另一个网页的 ViewPager
。
I'm not sure that I fully understand the question, but from the title of your question, I'm guessing that what you're looking for is pager.setCurrentItem( num )
. That allows you to programatically switch to another page within the ViewPager
.
我需要看到logcat的堆栈跟踪更具体的,如果这不是问题。
I'd need to see a stack trace from logcat to be more specific if this is not the problem.