NullPointerException一个调用标记片段ViewPager

时间:2013-12-17 06:25:10

标签: android android-fragments android-viewpager

需要帮助,尝试调用Activity的方法片段,我认为这是不正确的请求标签片段。

所以问一下标签:

public class TitleAdapter extends FragmentPagerAdapter {
public final String titles[] = new String[] { "A", "B", "C" };
public final Fragment frags[] = new Fragment[titles.length];



public TitleAdapter(FragmentManager fm) {
    super(fm);
    frags[0] = new FragmentA();
    fm.beginTransaction().add(frags[0], "fragA").commit();
    frags[1] = new FragmentB();
    fm.beginTransaction().add(frags[1], "fragB").commit();
    frags[2] = new FragmentC();
    fm.beginTransaction().add(frags[2], "fragC").commit();
}



@Override
public CharSequence getPageTitle(int position) {
    Log.v("TitleAdapter - getPageTitle=", titles[position]);
    return titles[position];
}


@Override
public Fragment getItem(int position) {

    Log.v("TitleAdapter - getItem=", String.valueOf(position));
    return frags[position];
}


@Override
public int getCount() {
    return frags.length;
}

}

感觉正确导致onCreate(Bundle savedInstanceState)

public class MainActivity extends FragmentActivity {

TitleAdapter titleAdapter;

ViewPager mViewPager;

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

    mViewPager = (ViewPager) findViewById(R.id.pager);

    TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager());
    mViewPager.setAdapter(titleAdapter);
    mViewPager.setCurrentItem(1);
    mViewPager.setOffscreenPageLimit(2);


    FragmentManager fm = getSupportFragmentManager();



    FragmentA fragment = (FragmentA)fm.findFragmentByTag("fragA");
    fragment.bgStop();

此处方法本身

public class FragmentA extends Fragment {

  final static String LOG_TAG = "myLogs";  
  static Button btnPlay;


    public FragmentA() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_a, container, false);

        btnPlay = (Button) rootView.findViewById(R.id.btnStart);




        return rootView;
    }





    public void bgPlay(){
               btnPlay.setText("Play");
           btnPlay.setBackgroundResource(R.drawable.play_button);
    }

    public void bgStop(){
               btnPlay.setText("Stop");
       btnPlay.setBackgroundResource(R.drawable.stop_button);
    }

}

1 个答案:

答案 0 :(得分:0)

该代码存在很多问题。在这里,让我为你解决这个问题。 以下是未经测试的,只是在我最喜欢的文本编辑器中编辑。

public class TitleAdapter extends FragmentPagerAdapter {
    public final String titles[] = new String[] { "A", "B", "C" };
    public final Fragment frags[] = new Fragment[titles.length];

    public TitleAdapter(FragmentManager fm) {
        super(fm);
        // do not commit any fragments here, the adapter handles that.
        frags[0] = new FragmentA();
        frags[1] = new FragmentB();
        frags[2] = new FragmentC();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Log.v("TitleAdapter - getPageTitle=", titles[position]);
        return titles[position];
    }

    @Override
    public Fragment getItem(int position) {

        Log.v("TitleAdapter - getItem=", String.valueOf(position));
        return frags[position];
    }

    @Override
    public int getCount() {
        return frags.length;
    }

}

然后在活动中:

public class MainActivity extends FragmentActivity {
    TitleAdapter titleAdapter;
    ViewPager mViewPager;

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

        mViewPager = (ViewPager) findViewById(R.id.pager);

        final TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(titleAdapter);
        mViewPager.setCurrentItem(1);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                Fragment fragment = titleAdapter.getItem(position);
                // Ideally use some sort of interface here.
                if (fragment instanceof FragmentA) {
                    ((FragmentA) fragment).bgStop();
                }
            }
        });
    }
}