ViewPager一个页面中包含多个片段" java.lang.IllegalArgumentException:如果没有查看发现ID" [英] ViewPager with one page containing multiple Fragments "java.lang.IllegalArgumentException: No view found for id"

查看:179
本文介绍了ViewPager一个页面中包含多个片段" java.lang.IllegalArgumentException:如果没有查看发现ID"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我小的Andr​​oid应用程序,我有一个 ViewPager 3个页面,每个页面包含一个片段,这是所有工作的罚款与 FragmentStatePagerAdapter (见前面的问题)。

In my small Android app, I have a ViewPager with 3 pages, each page contains one fragment, that's all working fine with a FragmentStatePagerAdapter (see earlier question).

现在的大屏幕,我想保持一个 ViewPager ,但我想前两个片段是相同的第一页。

Now for larger screens, I would like to keep a ViewPager, but I want the first two fragments to be on the same first page.

对于这一点,我实现自己的PagerAdapter,这主要是工作,我试图解决的唯一问题是,当我旋转屏幕,我得到一个错误:

For this, I implement my own PagerAdapter and this is mostly working, the only problem I am trying to resolve is when I am rotating the screen, I get an error:

12-11 16:34:13.526: ERROR/AndroidRuntime(9221): FATAL EXCEPTION: main
        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.matthieu/com.matthieu.TestActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f030002 (com.matthieu:id/top_fragment) for fragment TestFragment{481a3a98 #0 id=0x7f030002}
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815)
        at android.app.ActivityThread.access$2400(ActivityThread.java:125)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f030002 (com.matthieu:id/top_fragment) for fragment TestFragment{481a3a98 #0 id=0x7f030002}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:903)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
        at android.app.Activity.performStart(Activity.java:3781)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
        ... 12 more

我把样品code模拟什么,我试图做(并显示此错误消息)在 GitHub上

我想一个问题,我是因为片段的我在实际应用中,我真的不能有一个静态的newInstance 的构造函数,而是在片段中创建未来与时间有'setRetainInstance(真);集。

I guess one problem I have is because of the Fragments I have in the real application, I cannot really have a static newInstance constructor, but instead the Fragments are created ahead of time and have 'setRetainInstance(true);' set.

我理解的id的的ViewGroup 将包含片段应层次的一部分,最终,我想,这是...

I understand the id of the viewGroup that will contain the fragment should be part of the hierarchy, and eventually, I guess, it is...

如果这有什么差别,我使用的是支持库和SherlockActionBar保持向后与旧设备的兼容性。

If it makes any difference, I am using the support library and the SherlockActionBar to maintain backwards compatibility with older devices.

在如何去任何想法?

修改:我发现,如果我从 FragmentManager 删除片段中的onSaveInstanceState 我没有收到任何崩溃了。所有的碎片都从头开始,这是不是我想要重新创建的,但我想我可能要做到这一点,以某种方式在以后恢复那些片段......

EDIT: I have found that if I remove the Fragments from the FragmentManager in onSaveInstanceState I am not getting any crash anymore. All the fragments are recreated from scratch which is not what I want, but I guess I might have to do this and somehow recover those fragments later....

推荐答案

好吧,即使我搬到了做事的另一种方式,我不喜欢把事情做不完整的......

All right, even though I moved to another way of doing things, I don't like to leave things incomplete...

去所建议的标记嵌套片段的方式。张贴在评论的链接去404,但我发现自己的例如code 有关嵌套片段。

Going the way of the nested Fragments as suggested by Mark. The link posted in the comments goes to 404, but I found your example code about nested fragments.

这几件事情我已经在路上了解到:

A few things I've learned on the way:

  • 嵌套片段不能有 setRetainInstance(真),否则将获得:

java.lang.IllegalStateException:不能保留嵌套在其他片段fragements

java.lang.IllegalStateException: Can't retain fragements that are nested in other fragments

这是一个错误的页面创建一个的FrameLayout ViewPager 持有片段。看在Android code(具体的 FragmentPagerAdapter ),我应该只使用 container.getId()要找到在哪里添加片段。看起来你不能做任何事情,在这个水平更复杂或会破。

It was a mistake to create a FrameLayout in the page of the ViewPager to hold the fragment. Looking in the Android code (and specifically the FragmentPagerAdapter), I should just use container.getId() to find where to add the Fragment. Looks like you cannot do anything more sophisticated at that level or it will break.

从我的理解,如果碎片保存一些信息(像我一样),需要一个很长的时间来加载,不同的方式来处理这个问题是:

From my understanding, if the fragments hold some information (like I do) that takes a long time to load, the different ways to handle this are:

  • 使用的onSaveInstanceState (preferred的方式?),但如果片段是嵌套不可能的。需要的框架时(空的)构造函数被调用。
  • 使用 setRetainInstance(真),在这种情况下, savedInstanceBundle 总是,但片段(如Java对象)都不会被破坏,它仍然会通过onAttach / onCreateView。
  • 找到一些其他的方式来保存和恢复你需要什么...
  • Use onSaveInstanceState (preferred way ?), but not possible if the Fragment is nested. The (empty) constructor will get called when needed by the framework.
  • Use setRetainInstance(true), in that case savedInstanceBundle is always null, but that Fragment (as Java object) will not be destroyed and it will still go through onAttach / onCreateView.
  • Find some other way to save and restore what you need...

不管怎样,我把使用嵌套的片段我的解决方案在 github上

Anyway, I pushed my solution using nested fragments on github

这篇关于ViewPager一个页面中包含多个片段" java.lang.IllegalArgumentException:如果没有查看发现ID"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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