Viewpager 显示正确的片段但未对其进行初始化

Viewpager showing correct fragment but not initializing it

我到处都看过,但在每个 post 中,问题似乎仍然存在。我已经使用 FragmentStatePageAdapter 实现了一个 viewpager。当片段在 UI 上正确显示时,当我尝试将值传递给片段时,应用程序崩溃,因为片段未初始化。为了检查,我在每个部分都放了一个日志,但只显示了第一个和第三个日志。这是我的代码。

代码

public class MainActivity extends FragmentActivity implements      OnDataReceivedListener, OnStateChangedListener {

    ViewPager viewPager;
    PagerAdapter pagerAdapter;
    private ImageView logo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        logo = (ImageView)findViewById(R.id.logo);

        viewPager = (ViewPager)findViewById(R.id.viewpager_first);
        pagerAdapter= new PagerAdapter(getSupportFragmentManager(),3);
        CircleIndicator indicator (CircleIndicator)findViewById(R.id.indicator);
        viewPager.setAdapter(pagerAdapter);
        indicator.setEnabled(true);
        indicator.setViewPager(viewPager);

        logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                flipApi.startScan();
            }
        });

    }


    class PagerAdapter extends FragmentStatePagerAdapter {

        int num;
        public PagerAdapter(FragmentManager fm, int x) {
            super(fm);
            this.num=x;
        }

        public Fragment currentFragment;

        @Override
        public Fragment getItem(int position) {
            //add new fragment here for homepage viewpager

            switch (position) {

                case 0:
                    currentFragment = new Calorie_Fragment();
                    Log.d("flip",1+"");
                    return currentFragment;


                case 1:
                    currentFragment = new HeartRate_Fragment();
                    Log.d("flip",2+"");
                    return currentFragment;


                case 2:
                    currentFragment = new Steps_Fragment();
                    Log.d("flip",3+"");
                    return currentFragment;


                default:
                    return null;

            }

        }

        @Override
        public int getCount() {
            return num;
        }

        public Fragment getCurrentFragment() {
            return  this.currentFragment;
        }
    }
}

这是我的日志:

04-19 17:43:40.374 30949-30949/com.example.riche.flip_final D/flip: 1
04-19 17:43:40.388 30949-30949/com.example.riche.flip_final D/flip: 2
04-19 17:43:41.386 30949-30949/com.example.riche.flip_final 
I/Choreographer:Skipped 61 frames!  The application may be doing too 
much work on its main thread.
04-19 17:43:44.539 30949-30949/com.example.riche.flip_final D/flip: 3
04-19 17:43:44.560 30949-30949/com.example.riche.flip_
final W/FragmentManager: moveToState: Fragment state for Steps_Fragment{33bf8e3 #2 id=0x7f0d0075} not updated inline; expected state 3 found 2
04-19 17:43:46.999 30949-30949/com.example.riche.flip_final D/flip: 1
04-19 17:43:47.008 30949-30949/com.example.riche.flip_final  W/FragmentManager: moveToState: Fragment state for Calorie_Fragment{208d45e #1  id=0x7f0d0075} not updated inline; expected state 3 found 2
04-19 17:43:48.105 30949-30949/com.example.riche.flip_final D/flip: 3
04-19 17:43:48.115 30949-30949/com.example.riche.flip_final W/FragmentManager: moveToState: Fragment state for Steps_Fragment{fd45d27 #2  id=0x7f0d0075} not updated inline; expected state 3 found 2

试试这个。 :)

 class PagerAdapter extends FragmentStatePagerAdapter {

 HeartRate_Fragment heartRate_fragment;
Calorie_Fragment calorie_fragment;
 Steps_Fragment steps_fragment;

int num;
public PagerAdapter(FragmentManager fm, int x) {
    super(fm);
    this.num=x;
}
@Override
public Fragment getItem(int position) {
    //add new fragment here for homepage viewpager

    switch (position) {

        case 0:
            if(heartRate_fragment == null)
            heartRate_fragment = new HeartRate_Fragment();
            Log.d("flip",1+"");
            return heartRate_fragment;


        case 1:
            if(calorie_fragment == null)
            calorie_fragment = new Calorie_Fragment();
            Log.d("flip",2+"");
            return calorie_fragment;


        case 2:
            if(steps_fragment == null)
            steps_fragment = new Steps_Fragment();
            Log.d("flip",3+"");
            return steps_fragment;


        default:
            return null;

    }

}

@Override
public int getCount() {
    return num;
}

public HeartRate_Fragment getHRFragment()
{
    return heartRate_fragment;
}
public Calorie_Fragment getCalorie_fragment()
{
    return calorie_fragment;
}
public  Steps_Fragment getSteps_fragment()
{
    return steps_fragment;
}