滑动选项卡ViewPager时出现NullPointerException

时间:2014-08-30 14:46:07

标签: android fragmentpageradapter

在标签之间滑动时,我的ViewPager会崩溃。它返回NullPointerException。这是当我尝试从一个选项卡滑动到另一个选项卡时发生的堆栈跟踪。

08-30 10:46:54.524: E/AndroidRuntime(9877): FATAL EXCEPTION: main
08-30 10:46:54.524: E/AndroidRuntime(9877): Process: com.justein.testioutracker, PID: 9877
08-30 10:46:54.524: E/AndroidRuntime(9877): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
08-30 10:46:54.524: E/AndroidRuntime(9877):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at java.util.ArrayList.get(ArrayList.java:308)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at com.android.internal.app.ActionBarImpl.setSelectedNavigationItem(ActionBarImpl.java:363)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at com.justein.testioutracker.IOUFragmentActivity$1.onPageScrollStateChanged(IOUFragmentActivity.java:48)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.support.v4.view.ViewPager.setScrollState(ViewPager.java:403)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.support.v4.view.ViewPager.smoothScrollTo(ViewPager.java:811)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:570)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:556)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at com.justein.testioutracker.IOUFragmentActivity.onTabSelected(IOUFragmentActivity.java:62)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at com.android.internal.app.ActionBarImpl.setSelectedNavigationItem(ActionBarImpl.java:363)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at com.justein.testioutracker.IOUFragmentActivity$1.onPageScrollStateChanged(IOUFragmentActivity.java:48)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.support.v4.view.ViewPager.setScrollState(ViewPager.java:403)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1858)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1859)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2216)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1959)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2107)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1519)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.app.Activity.dispatchTouchEvent(Activity.java:2458)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2043)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.View.dispatchPointerEvent(View.java:7911)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4130)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3997)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3537)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3593)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3562)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3669)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3570)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3726)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3543)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3593)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3562)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3570)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3543)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5740)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5713)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5676)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5820)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:215)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:201)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:5793)
08-30 10:46:54.524: E/AndroidRuntime(9877):     at android.view.ViewRootImpl$ConsumeBatchedInpu

这是我的FragmentPagerAdapter类:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class FragmentPageAdapter extends FragmentPagerAdapter {

    public FragmentPageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        switch(arg0) {
        case 0:
            return new OweFragment();
        case 1:
            return new OwedFragment();
        default:
            return null;
        }
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 2;
    }

}

这是我的FragmentActivity:

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class IOUFragmentActivity extends FragmentActivity implements ActionBar.TabListener {
    private ActionBar actionBar;
    private String name;
    private FragmentPageAdapter ft;
    private ViewPager viewPager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ioufragment);
        ft = new FragmentPageAdapter(getSupportFragmentManager());
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(ft);
        Intent intent = getIntent();
        name = intent.getStringExtra("name");
        actionBar = getActionBar();
        actionBar.setTitle(name);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.addTab(actionBar.newTab().setText("Owe").setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText("Owed").setTabListener(this));
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
//              actionBar.setSelectedNavigationItem(arg0);

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                actionBar.setSelectedNavigationItem(arg0);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        getMenuInflater().inflate(R.menu.actionbar_menu, menu);
        return true;
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition()); 
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
}

如果需要更多代码来找出答案,请告诉我,我将发布我的ListFragment类。

1 个答案:

答案 0 :(得分:0)

我猜问题出现在onPageScrollStateChanged上。请注意,它不接受页面索引而是接受状态标志。刚刚看到你已经更新过的电话堆栈就是这样......

onPageScrollStateChanged