2015-12-13 64 views
0

因此,當用戶在每個頁面上滑動時,我想要在最後啓動一個意圖;展示主要活動。現在,我將頁數設置爲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; 

} 
} 
+0

什麼是這次崩潰的堆棧跟蹤? –

+0

'if(position == 4){'這需要3,從0..3開始給你4頁 –

+0

我認爲解決這個問題的方法是調用某種outofbounds監聽器。但是,我無法找到一個 –

回答

0

更改驗證啓動Intent 3,不是4 記住名單開始0

統計指標
@Override 
public Fragment getItem(int position) { 

    if (position == 3) { // position 3 is the page #4 

     Intent intent = new Intent(getApplication(), MainActivity.class); 
     startActivity(intent); 
     finish(); 
    } 

    return ScreenSlidePageFragment.initialize(position); 
} 

您收到的錯誤e是你打電話給ScreenSlidePageFragment.initialize(3)。在ScreenSlidePageFragment中,String[] textint[] images包含3個元素(來自索引0..2)。

這會在運行時造成ArrayIndexOutOfBoundsException並導致應用程序崩潰。

+0

這樣做不起作用。由於位置3超出界限,我將永遠無法滑動到達該位置。有沒有像某種方式,當我滑出界限,意圖將啓動? –

+0

爲什麼出界?你沒有4頁嗎? –

+0

我只有3頁。當我滑到第四(這不存在),我想要啓動的意圖。這就是爲什麼最初我將頁數設置爲4,以便當我刷到不存在的第4頁時,意圖將運行 –