多ViewPagers内单的ListView崩溃 [英] Multiple ViewPagers Inside Single ListView is Crashing

查看:495
本文介绍了多ViewPagers内单的ListView崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在刷卡通过Viewpagers其正常使用,我曾用做 SETID()方法 ViewPagers getView()方式的<强> BaseAdapter

code实现的是类似这样的主题,我发现在这里: 对这个职位

刚刚加入SETID创建每个ViewPager的对象后(位置+ 1)。

但是,当我开始通过列表滚动(水平)的应用是越来越坠毁显示低于错误的logcat的:

  

06-18 21:06:55.020:E / AndroidRuntime(1928年):致命异常:主要   06-18 21:06:55.020:E / AndroidRuntime(1928年):   android.content.res.Resources $ NotFoundException:无法找到   资源编号为0x7 06-18 21:06:55.020:E / AndroidRuntime(1928年):在   android.content.res.Resources.getResourceName(Resources.java:1653)   06-18 21:06:55.020:E / AndroidRuntime(1928年):在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)   06-18 21:06:55.020:E / AndroidRuntime(1928年):在   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)   06-18 21:06:55.020:E / AndroidRuntime(1928年):在   android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)   06-18 21:06:55.020:E / AndroidRuntime(1928年):在   android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)   06-18 21:06:55.020:E / AndroidRuntime(1928年):在   android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)   06-18 21:06:55.020:E / AndroidRuntime(1928年):在   android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)06-18 21:06:55.020:E / AndroidRuntime(1928年):atandroid.support.v4.view.ViewPager.populate(ViewPager.java: 1064)06-18   21:06:55.020:E / AndroidRuntime(1928年):在   android.support.v4.view.ViewPager.populate(ViewPager.java:911)06-18   21:06:55.020:E / AndroidRuntime(1928年):在   android.support.v4.view.ViewPager.setAdapter(ViewPager.java:440)06-18   21:06:55.020:E / AndroidRuntime(1928年):在   com.example.democardswipelistview.CustomSwipeAdapter.getView(CustomSwipeAdapter.java:72)   06-18 21:06:55.020:E / AndroidRuntime(1928年):在   android.widget.AbsListView.obtainView(AbsListView.java:2012)06-18   21:06:55.020:E / AndroidRuntime(1928年):在   android.widget.ListView.makeAndAddView(ListView.java:1772)06-18   21:06:55.020:E / AndroidRuntime(1928年):在   android.widget.ListView.fillUp(ListView.java:705)06-18 21:06:55.020:   E / AndroidRuntime(1928年):在   android.widget.ListView.fillGap(ListView.java:645)06-18 21:06:55.020:   E / AndroidRuntime(1928年):在   android.widget.AbsListView.trackMotionScroll(AbsListView.java:4546)   06-18 21:06:55.020:E / AndroidRuntime(1928年):在   android.widget.AbsListView $ FlingRunnable.run(AbsListView.java:3813)   06-18 21:06:55.020:E / AndroidRuntime(1928年):在   android.os.Handler.handleCallback(Handler.java:605)06-18   21:06:55.020:E / AndroidRuntime(1928年):在   android.os.Handler.dispatchMessage(Handler.java:92)06-18   21:06:55.020:E / AndroidRuntime(1928年):在   android.os.Looper.loop(Looper.java:137)06-18 21:06:55.020:   E / AndroidRuntime(1928年):在   android.app.ActivityThread.main(ActivityThread.java:4424)06-18   21:06:55.020:E / AndroidRuntime(1928年):在   java.lang.reflect.Method.invokeNative(本机方法)06-18   21:06:55.020:E / AndroidRuntime(1928年):在   java.lang.reflect.Method.invoke(Method.java:511)06-18 21:06:55.020:   E / AndroidRuntime(1928年):在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)   06-18 21:06:55.020:E / AndroidRuntime(1928年):在   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)06-18   21:06:55.020:E / AndroidRuntime(1928年):在   dalvik.system.NativeStart.main(本机方法)

解决方案

好了,所以后第2天,我能够解决类似的问题是这样的。

先不要坚持你的 ViewPager getView()。我的意思是,你不应该叫 convertView.setTag(持有人); 持有人=(ViewHolder)convertView.getTag()。只是删除if语句来检查,如果 convertView 为空。然后,完全删除else语句块。

另外一个变化,使(这是不是原因之一)是您需要优化您设置的ID的方式。 而不是 SETID(位置+ 1),使一个Ids.xml文件在你的价值观的文件夹,则该文件中,把这样的:

 &LT; XML版本=1.0编码=UTF-8&GT?;

&LT;资源&GT;
  &LT;! - 使用产​​生不冲突的id为viewpager  - &GT;

  &LT;项目名称=viewpager_id_baseTYPE =ID/&GT;
&LT; /资源&GT;
 

然后在你的 getView()方法替换 SETID(位置+ 1) SETID(R.id.viewpager_id_base +位置)

上述变更后,该列表将不会崩溃,但经过滚动的项目关闭屏幕就会消失。下面是该修正:

请确保您的自定义 PageAdapter 扩展 FragmentStatePagerAdapter ,不是 FragmentPagerAdapter 。这将破坏正常的意见,并触发onCreateView()。

When Swipe through Viewpagers its Working Perfectly which i had done using setId() method for ViewPagers on getView() method of BaseAdapter.

Code Of Implementation is similar to this thread, which i found here: On This Post

Just added setId(position+1) after creating each ViewPager's Object.

But when i start scrolling through List (Horizontally) Application is getting crashed showing below Error in Logcat:

06-18 21:06:55.020: E/AndroidRuntime(1928): FATAL EXCEPTION: main 06-18 21:06:55.020: E/AndroidRuntime(1928): android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.content.res.Resources.getResourceName(Resources.java:1653) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141) 06-18 21:06:55.020: E/AndroidRuntime(1928): atandroid.support.v4.view.ViewPager.populate(ViewPager.java:1064) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.support.v4.view.ViewPager.populate(ViewPager.java:911) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:440) 06-18 21:06:55.020: E/AndroidRuntime(1928): at com.example.democardswipelistview.CustomSwipeAdapter.getView(CustomSwipeAdapter.java:72) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.widget.AbsListView.obtainView(AbsListView.java:2012) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.widget.ListView.makeAndAddView(ListView.java:1772) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.widget.ListView.fillUp(ListView.java:705) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.widget.ListView.fillGap(ListView.java:645) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4546) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3813) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.os.Handler.handleCallback(Handler.java:605) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.os.Handler.dispatchMessage(Handler.java:92) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.os.Looper.loop(Looper.java:137) 06-18 21:06:55.020: E/AndroidRuntime(1928): at android.app.ActivityThread.main(ActivityThread.java:4424) 06-18 21:06:55.020: E/AndroidRuntime(1928): at java.lang.reflect.Method.invokeNative(Native Method) 06-18 21:06:55.020: E/AndroidRuntime(1928): at java.lang.reflect.Method.invoke(Method.java:511) 06-18 21:06:55.020: E/AndroidRuntime(1928): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-18 21:06:55.020: E/AndroidRuntime(1928): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-18 21:06:55.020: E/AndroidRuntime(1928): at dalvik.system.NativeStart.main(Native Method)

解决方案

Ok so after 2 days, I was able to solve a similar issue like this.

First do not persist your ViewPager in the getView(). What I mean by this is that you shouldn't call convertView.setTag(holder); or holder = (ViewHolder)convertView.getTag(). Just remove the if statement that checks if convertView is null. Then completely remove the else statement block.

Another change to make (this is not one of the causes) is you need to optimize the way you set the Ids. Instead of setId(position+1), make an Ids.xml file in your values folder, then inside that file, put this:

<?xml version="1.0" encoding="utf-8"?>

<resources>
  <!-- use to generate non-conflicting ids for the viewpager -->

  <item name="viewpager_id_base" type="id"/>
</resources>

Then in your getView() method replace setId(position+1) with setId(R.id.viewpager_id_base + position)

After the above change, the list will not crash, but it will disappear after you scroll the item off screen. Here is the fix for that:

Make sure your Custom PageAdapter extends FragmentStatePagerAdapter, not FragmentPagerAdapter. This will properly destroy the views and trigger the onCreateView().

这篇关于多ViewPagers内单的ListView崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆