ViewPager getChildAt onPageSelected更新数据错误

时间:2014-11-20 11:31:54

标签: android android-fragments android-viewpager

我在Activity类中有片段内部类:

public class MyViewPager extends Fragment implements OnPageChangeListener {

    private FragmentManager fm;
    private ViewPager pager;
    private SquareAdapter FragmentSquareAdapter;
    TextView pifNumber = null;
    public MyViewPager(FragmentManager fm) {
        this.fm = fm;

    }

    public void setCurrentItem(int item) {
        pager.setCurrentItem(item);
    }

    public int getCurrentItem() {
        return pager.getCurrentItem();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub  
        super.onActivityCreated(savedInstanceState);
        FragmentSquareAdapter = new SquareAdapter(getChildFragmentManager());
        pager.setAdapter(FragmentSquareAdapter);
        pager.setOnPageChangeListener(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.my_view_pager, container,
                false);
        pager = (ViewPager) root.findViewById(R.id.pager);
        return root;
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

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

    }

    @Override
    public void onPageSelected(int arg0) {
        // TODO Auto-generated method stub
        if (arg0 == 0) {
            getSquare();
            mDrawerToggle.setDrawerIndicatorEnabled(true);
            LeftDrawer
                    .setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        } else {

            pifNumber = (TextView) pager.getChildAt(arg0).findViewById(R.id.pifagorSaysText);
            //TextView pifNumber = (TextView) pager.findViewById(R.id.pifagorSaysText);
            //pifNumber.setText(pifagor.getNumbersForSquare(arg0));
            mDrawerToggle.setDrawerIndicatorEnabled(false);
            TextView tf = (TextView) findViewById(R.id.textView1);
            tf.setText(json_object.toString());
            LeftDrawer
                    .setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        }   

    }
}

因此,ViewPager的适配器看起来像:

public class SquareAdapter extends FragmentStatePagerAdapter {

public SquareAdapter(FragmentManager fragmentManager) {
    super(fragmentManager);
    // TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int arg0) {
    // TODO Auto-generated method stub
    return (EditorFragment.newInstance(arg0));
}

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

}

EditorFragment看起来像:

public class EditorFragment extends Fragment{
private static final String KEY_POSITION = "position";
    private TextView pifagorSaysText;
static EditorFragment newInstance(int position) {
    EditorFragment frag = new EditorFragment();
    Bundle args = new Bundle();

    args.putInt(KEY_POSITION, position);
    frag.setArguments(args);

    return (frag);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    int position = getArguments().getInt(KEY_POSITION, -1);
    View result = null;
    if (position == 0) {
        result = inflater.inflate(R.layout.editor, container, false);
    } else {
        result = inflater.inflate(R.layout.editor_about, container, false);
        pifagorSaysText = (TextView)result.findViewById(R.id.pifagorSaysText);
        Log.d("PAGER", " "+(pifagorSaysText==null));
        Log.d("PAGER", " position"+position);       
    }
    return result;
}

public int getPosition(){
    int position = getArguments().getInt(KEY_POSITION, -1);
    return position;
}

}

那么,我需要什么?我需要在R.id.editor_about中的TextView中设置文本,该文本在EditorFragment类中膨胀。当我想在我的内部片段类中获取此TextView时,我在前2个片段中设置文本,而不是我有错误:

11-20 14:28:25.279: E/InputEventReceiver(2648): Exception dispatching input event.
11-20 14:28:25.279: E/MessageQueue-JNI(2648): Exception in MessageQueue callback:     handleReceiveCallback
11-20 14:28:25.299: E/MessageQueue-JNI(2648): java.lang.NullPointerException
11-20 14:28:25.299: E/MessageQueue-JNI(2648):   at  com.example.pifsquare.Square$MyViewPager.onPageSelected(Square.java:469)
11-20 14:28:25.299: E/MessageQueue-JNI(2648):   at  android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:572)
11-20 14:28:25.299: E/MessageQueue-JNI(2648):   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:556)
11-20 14:28:25.299: E/MessageQueue-JNI(2648):   at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2020)

public void getSquare() {
    Typeface typeface = Typeface.createFromAsset(this.getAssets(),
            "fonts/AcquestScript.ttf");
    List<Button> btns = new ArrayList<Button>();
    btns.add((Button) findViewById(R.id.button1));
    btns.add((Button) findViewById(R.id.button2));
    btns.add((Button) findViewById(R.id.button3));
    btns.add((Button) findViewById(R.id.button4));
    btns.add((Button) findViewById(R.id.button5));
    btns.add((Button) findViewById(R.id.button6));
    btns.add((Button) findViewById(R.id.button7));
    btns.add((Button) findViewById(R.id.button8));
    btns.add((Button) findViewById(R.id.button9));
    for (Button b : btns) {
        if (b != null) {
            String numbers = pifagor.getNumbersForSquare(Integer
                    .valueOf((String) b.getTag()));
            b.setText(numbers);
            b.setTypeface(typeface);
            if (numbers.length() > 4) {
                b.setTextSize(45);
            } else {
                b.setTextSize(55);
            }
        }
    }
}

0 个答案:

没有答案