0

这是我的代码:

.......
CollectionPagerAdapter mCollectionPagerAdapter;
ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {  Log.i("App", "U reaching Da onCreate");       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_fragment);


     mCollectionPagerAdapter =
                new CollectionPagerAdapter(
                        getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.CardsPager);
        mViewPager.setAdapter(mCollectionPagerAdapter);
  ....

    //ADAPTER:
class CollectionPagerAdapter extends FragmentStatePagerAdapter {
    public CollectionPagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new ObjectFragment();
        Bundle args = new Bundle();
        // Our object is just an integer :-P
        args.putInt(ObjectFragment.ARG_OBJECT, i + 1);
        fragment.setArguments(args);
        Log.i("App", "U reaching Da Adapter!!");
        return fragment;
    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
}
 .....
//ObjectFragment.java
public class ObjectFragment extends Fragment {
    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        View rootView = inflater.inflate(
            R.layout.cards_pager, container, false);
        Bundle args = getArguments();
        ((TextView) rootView.findViewById(android.R.id.text1)).setText(
            Integer.toString(args.getInt(ARG_OBJECT)));
        Log.i("App", "U reaching Da inflater Y U no work?!");
        return rootView;
    }
}

main_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="40"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:background="#109922" >

    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="20"
    android:orientation="vertical" 
    android:background="#115599">

     <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/CardsPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000000" >

     </android.support.v4.view.ViewPager>



</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="40"
    android:orientation="vertical" >



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" 
            android:background="#ff9922">
        </LinearLayout>

</LinearLayout>

似乎应用程序永远不会到达适配器。

我完全按照 android 开发人员指南进行操作。我不知道这里发生了什么,eclipse让我运行应用程序,代码中的一切似乎都很好,但它只是不起作用

4

1 回答 1

0

我终于弄对了,问题是我在onCreate()方法中设置了适配器,为了解决这个问题我在MainFragment的onCreateView()方法中设置了适配器。

于 2013-07-09T20:27:29.557 回答