ViewPager作为listview行java.lang.IllegalArgumentException:找不到id的视图

时间:2014-10-28 09:30:30

标签: android listview

我试图将viwepager作为listview中的行。它工作,我可以滚动和垂直滚动。 enter image description here

滚动后我得到java.lang.IllegalArgumentException: No view found for id...。下面是我的鳕鱼和错误日志。

我的适配器

  private class ExpAdapter
            extends CursorAdapter {

        public ExpAdapter(Context context, FragmentManager mFragmentManager) {
            super(context, null, 0);
            this.mFragmentManager = mFragmentManager;
            mInflater = LayoutInflater.from(context);
        }


        private FragmentManager mFragmentManager;
        LayoutInflater mInflater;
        private Date d = new Date();


        @Override
        public View newView(
                Context context, Cursor cursor, ViewGroup parent
        ) {
            ViewPager pager = (ViewPager) mInflater.inflate(
                    R.layout.view_pager, parent, false
            );

            pager.setId(R.id.viewpager_id_base + cursor.getPosition());
            return pager;
        }


        @Override
        public void bindView(
                View view, Context context, Cursor cursor
        ) {
            // make pagerCoursor
            ScreenSlidePagerAdapter tempMyFriendPagerAdapter = new ScreenSlidePagerAdapter(
                    mFragmentManager, pagerCoursor
            );
            ((ViewPager) view).setAdapter(tempMyFriendPagerAdapter);
        }

    }

XML

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                                   android:layout_width="match_parent"
                                   android:layout_height="400dip"/>

错误

 java.lang.IllegalArgumentException: No view found for id 0x7f070000 (com.yarh.bigdig.smsgreeter:id/viewpager_id_base) for fragment ScreenSlidePageFragment{41cfea70 #1 id=0x7f070000}
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:939)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
            at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:447)
            at com.yarh.bigdig.smsgreeter.ui.CelebrationListFragment$ExpAdapter.bindView(CelebrationListFragment.java:367)
            at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
            at android.widget.AbsListView.obtainView(AbsListView.java:2435)
            at android.widget.ListView.makeAndAddView(ListView.java:1891)
            at android.widget.ListView.fillDown(ListView.java:792)
            at android.widget.ListView.fillGap(ListView.java:756)
            at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5718)
            at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4816)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:799)
            at android.view.Choreographer.doCallbacks(Choreographer.java:599)
            at android.view.Choreographer.doFrame(Choreographer.java:558)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:784)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5872)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
            at dalvik.system.NativeStart.main(Native Method)

我认为eror是由id冲突引起的,所以我试图替换

  

pager.setId(R.id.viewpager_id_base + cursor.getPosition());

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        myView.setId(Utils.generateViewId());
} else {
        myView.setId(View.generateViewId());
}

如建议的here,但它显示了我的免费资源创建

1 个答案:

答案 0 :(得分:0)

对我而言,关键是清除所有线性布局的孩子并在每次适配器调用getView时设置新的viewpager

 @Override
            public void bindView(
                    View view, Context context, Cursor cursor
            ) {
                final String date = cursor.getString(1);
                String[] projection =

                        {CelebrationTable.TABLE_CELEBRATION + "." + CelebrationTable.COLUMN_ID

                        };

                String
                        selection =
                        " and strftime(" + dateLimiter + ", " + CelebrationTable.TABLE_CELEBRATION + "." + CelebrationTable.COLUMN_DATE + " / 1000, 'unixepoch') = '" + date + "'";


                String
                        orderBy =
                        CelebrationTable.TABLE_CELEBRATION + "." + CelebrationTable.COLUMN_DATE + " ASC";


                Cursor
                        pagerCoursor =
                        context.getContentResolver()
                                .query(
                                        CelebrationContentProvider.CONTENT_URI,
                                        projection,
                                        selection,
                                        null,
                                        orderBy
                                );

                ViewPager pager = new ViewPager(mInflater.getContext());
                pager.setLayoutParams(
                        new ViewGroup.LayoutParams(
                                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
                        )
                );
                ViewHolder holder = (ViewHolder) view.getTag();
                holder.ll.removeAllViews();
                holder.ll.addView(pager);

                pager.setId(idArrays[cursor.getPosition()]);
                int curPos = cursor.getPosition();
                ArrayList<Long> celebrationIds = new ArrayList<>();
                while (pagerCoursor.moveToNext()){
                    celebrationIds.add(pagerCoursor.getLong(0));
                }

                ScreenSlidePagerAdapter tempMyFriendPagerAdapter = new ScreenSlidePagerAdapter(
                        mFragmentManager, celebrationIds, curPos
                );
                pagerCoursor.close();
                pager.setAdapter(tempMyFriendPagerAdapter);
            }