Multiple ViewPagers Inside Single ListView is Crashing(单个 ListView 内的多个 ViewPagers 崩溃)
问题描述
当通过 Viewpagers 滑动时,它的工作完美,我在 getView() 方法上使用 ViewPagers 的 setId() 方法完成了>BaseAdapter.
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
在创建每个 ViewPager 的 Object 后添加 setId(position+1).
Just added setId(position+1) after creating each ViewPager's Object.
但是当我开始滚动列表(水平)时,应用程序崩溃,显示以下 Logcat 中的错误:
06-18 21:06:55.020: E/AndroidRuntime(1928): 致命异常: main06-18 21:06:55.020: E/AndroidRuntime(1928):android.content.res.Resources$NotFoundException:找不到资源 ID #0x7 06-18 21:06:55.020: E/AndroidRuntime(1928): atandroid.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-1821:06:55.020:E/AndroidRuntime(1928):在android.support.v4.view.ViewPager.populate(ViewPager.java:911) 06-1821:06:55.020:E/AndroidRuntime(1928):在android.support.v4.view.ViewPager.setAdapter(ViewPager.java:440) 06-1821: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-1821:06:55.020:E/AndroidRuntime(1928):在android.widget.ListView.makeAndAddView(ListView.java:1772) 06-1821: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-1821:06:55.020:E/AndroidRuntime(1928):在android.os.Handler.dispatchMessage(Handler.java:92) 06-1821: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-1821:06:55.020:E/AndroidRuntime(1928):在java.lang.reflect.Method.invokeNative(Native Method) 06-1821: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-1821:06:55.020:E/AndroidRuntime(1928):在dalvik.system.NativeStart.main(Native Method)
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)
推荐答案
好的,2天后,我能够解决类似的问题.
Ok so after 2 days, I was able to solve a similar issue like this.
首先不要在 getView()
中持久化你的 ViewPager
.我的意思是你不应该调用 convertView.setTag(holder);
或 holder = (ViewHolder)convertView.getTag()
.只需删除检查 convertView
是否为空的 if 语句.然后完全删除 else 语句块.
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.
要进行的另一项更改(这不是原因之一)是您需要优化设置 ID 的方式.而不是 setId(position+1)
,在你的 values 文件夹中创建一个 Ids.xml 文件,然后在该文件中,放入:
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>
然后在您的 getView()
方法中将 setId(position+1)
替换为 setId(R.id.viewpager_id_base + position)
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:
确保您的自定义 PageAdapter
扩展了 FragmentStatePagerAdapter
,而不是 FragmentPagerAdapter
.这将正确销毁视图并触发 onCreateView().
Make sure your Custom PageAdapter
extends FragmentStatePagerAdapter
, not FragmentPagerAdapter
. This will properly destroy the views and trigger the onCreateView().
这篇关于单个 ListView 内的多个 ViewPagers 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:单个 ListView 内的多个 ViewPagers 崩溃
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01