在onResume事件上找不到ListView + ViewPager资源异常

时间:2014-08-26 14:52:00

标签: android android-listview crash android-viewpager android-logcat

我的申请中有一个令人沮丧的问题:

  • 我有一个片段,其中包含一个 ListView
  • ListView 的每一行都会从XML文件中加载 ViewPager ViewPagerCustomIndicator 对象。
  • 对于每个 ViewPager 已加载,我设置了一个Id by viewHolder.viewPager.setId(Utils.generateViewId()); ;我找到了这个提示here
  • onResume Fragment 事件加载 ListView 数据。

第一次时,一切正常正常。在第二次加载时,应用崩溃包含此日志:

  

08-26 15:39:05.441:E / AndroidRuntime(31633):流程:   com.playyoo.android.sportyes,PID:31633 08-26 15:39:05.441:   E / AndroidRuntime(31633):   的> android.content.res.Resources $ NotFoundException:无法找到   资源ID#0x1 08-26 15:39:05.441: E / AndroidRuntime(31633):at   android.content.res.Resources.getResourceName(Resources.java:1773)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:934)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1103)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1901)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1518)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:962)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:764)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1533)08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.FragmentManagerImpl $ 2.run(FragmentManager.java:489)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:450)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   android.os.Handler.handleCallback(Handler.java:733)08-26   15:39:05.441:E / AndroidRuntime(31633):at   android.os.Handler.dispatchMessage(Handler.java:95)08-26   15:39:05.441:E / AndroidRuntime(31633):at   android.os.Looper.loop(Looper.java:136)08-26 15:39:05.441:   E / AndroidRuntime(31633):at   android.app.ActivityThread.main(ActivityThread.java:5001)08-26   15:39:05.441:E / AndroidRuntime(31633):at   java.lang.reflect.Method.invoke(Native Method)08-26 15:39:05.441:   E / AndroidRuntime(31633):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)   08-26 15:39:05.441:E / AndroidRuntime(31633):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

好的错误显而易见,但我找不到解决方法来解决它。如果没有 ViewPager ,应用程序就可以正常运行。

这是我的适配器(在此示例中我不使用Android回收单元机制):

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        XmlEvento e = (XmlEvento) getItem(position);

        final ViewHolder viewHolder = new ViewHolder();

        View view = _mInflater.inflate(_graphicResource, null);
        viewHolder.viewPager = (ViewPager) view
                .findViewById(R.id.live_view_pager);
        viewHolder.viewPagerIndicator = (ViewPagerIndicator) view
                .findViewById(R.id.live_view_pager_indicator);
        // viewHolder.viewPager.setId(position + 1);
        viewHolder.viewPager.setId(Utils.generateViewId());

        view.setTag(viewHolder);

        final List<Fragment> fragments = new Vector<Fragment>();

        for (int i = 0; i < scommesse.size(); i++) {

            XmlScommesse scommmessa = scommesse.get(i);

            Fragment fr = Live_Level_1_BetRow_Fragment.newInstance(e,
                    scommmessa);
            fragments.add(fr);
        }

            final String eId = getEventId(e);
            final Live_Bet_Level_1_Adapter mPagerAdapter = new Live_Bet_Level_1_Adapter(
                    _fragmentManager, fragments, _iBet);

            viewHolder.viewPager.setAdapter(mPagerAdapter);
            viewHolder.viewPager.setCurrentItem(_viewPagerStoredPosition
                    .get(eId));

                // Bind the circle indicator to the adapter.
                viewHolder.viewPagerIndicator
                        .setViewPager(viewHolder.viewPager);
                viewHolder.viewPagerIndicator
                        .setOnPageChangeListener(new OnPageChangeListener() {

                            @Override
                            public void onPageSelected(int p) {

                                Live_Level_1_BetRow_Fragment fr = (Live_Level_1_BetRow_Fragment) fragments
                                        .get(p);
                                fr.setAdapter(mPagerAdapter);
                            }

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

                            @Override
                            public void onPageScrolled(int position,
                                    float positionOffset,
                                    int positionOffsetPixels) {
                                // TODO Auto-generated method stub
                            }
                        });

Live_Level_1_BetRow_Fragment.newInstance(e,                         bet); 是一个简单的片段,其中包含:

  

view = inflater.inflate(R.layout.MY_ROW_LAYOUT,container,false);

感谢您的建议。

0 个答案:

没有答案