2

我的代码:

主要活动

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

..
mAdapter = new MyAdapter(getFragmentManager());
            mPager = (ViewPager) findViewById(R.id.pager);
            mPager.setAdapter(mAdapter);
            mPager.setOnPageChangeListener(new MyPageChangeListener());


private class MyPageChangeListener extends
            ViewPager.SimpleOnPageChangeListener {
        @Override
        public void onPageSelected(int position) {
            focusedPage = position;
        }
    }

public class MyAdapter extends FragmentStatePagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

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

    @Override
    public Fragment getItem(int position) {
        ArrayListFragment alf = new ArrayListFragment();
        return alf.newInstance(position);
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position,
            Object object) {
        ArrayListFragment alf = (ArrayListFragment) object;
        mCurrentView = alf.getView();

    }

}

public class ArrayListFragment extends Fragment {
    int mNum;


    /**
     * Create a new instance of CountingFragment, providing "num" as an
     * argument.
     */
    ArrayListFragment newInstance(int num) {
        ArrayListFragment f = new ArrayListFragment();

        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putInt("num", num);
        f.setArguments(args);

        return f;
    }

    /**
     * When creating, retrieve this instance's number from its arguments.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
    }

当 savedInstanceState 为 null 时(例如当用户重新开始时)一切正常。但是当不为空时,我得到了这个异常:

04-29 14:10:00.398: E/AndroidRuntime(29348): 由: android.app.Fragment$InstantiationException: 无法实例化片段 com.Android.Prj.DispKl$ArrayListFragment: 确保类名存在,是公开的,并且有一个公开的空构造函数

为什么会这样,我该如何解决?

4

1 回答 1

2

如果ArrayListFragment是 的内部类com.Android.Prj.DispKl,则必须是static。或者,将其移动到自己独立的公共 Java 类中。

顺便说一句,请不要将代码放入com.Android包中。请使用您自己的域名。

于 2012-04-29T16:00:59.763 回答