<legend id='iPSnQ'><style id='iPSnQ'><dir id='iPSnQ'><q id='iPSnQ'></q></dir></style></legend>
      <bdo id='iPSnQ'></bdo><ul id='iPSnQ'></ul>
    <tfoot id='iPSnQ'></tfoot>

    <small id='iPSnQ'></small><noframes id='iPSnQ'>

    1. <i id='iPSnQ'><tr id='iPSnQ'><dt id='iPSnQ'><q id='iPSnQ'><span id='iPSnQ'><b id='iPSnQ'><form id='iPSnQ'><ins id='iPSnQ'></ins><ul id='iPSnQ'></ul><sub id='iPSnQ'></sub></form><legend id='iPSnQ'></legend><bdo id='iPSnQ'><pre id='iPSnQ'><center id='iPSnQ'></center></pre></bdo></b><th id='iPSnQ'></th></span></q></dt></tr></i><div id='iPSnQ'><tfoot id='iPSnQ'></tfoot><dl id='iPSnQ'><fieldset id='iPSnQ'></fieldset></dl></div>

      DialogFragment 中的 ViewPager - IllegalStateException:片段没有视图

      时间:2023-10-04
      <legend id='7HUfj'><style id='7HUfj'><dir id='7HUfj'><q id='7HUfj'></q></dir></style></legend>

      <tfoot id='7HUfj'></tfoot>

    2. <i id='7HUfj'><tr id='7HUfj'><dt id='7HUfj'><q id='7HUfj'><span id='7HUfj'><b id='7HUfj'><form id='7HUfj'><ins id='7HUfj'></ins><ul id='7HUfj'></ul><sub id='7HUfj'></sub></form><legend id='7HUfj'></legend><bdo id='7HUfj'><pre id='7HUfj'><center id='7HUfj'></center></pre></bdo></b><th id='7HUfj'></th></span></q></dt></tr></i><div id='7HUfj'><tfoot id='7HUfj'></tfoot><dl id='7HUfj'><fieldset id='7HUfj'></fieldset></dl></div>
            <bdo id='7HUfj'></bdo><ul id='7HUfj'></ul>

            1. <small id='7HUfj'></small><noframes id='7HUfj'>

                <tbody id='7HUfj'></tbody>

              • 本文介绍了DialogFragment 中的 ViewPager - IllegalStateException:片段没有视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                • FragmentActivity 在单击操作栏中的操作按钮时显示一个对话框
                • DialogFragment - 没有标题的对话框
                • TabHost - 对话框顶部的选项卡
                • ViewPagerFragmentPagerAdapter - 可滑动,内容连接到 Tabs
                • 2-3 Dialog Buttons(Dialog的不同子类,不同的按钮)——不应该在ViewPagerFragment之一中,意义相同无论 ViewPager 显示什么 Fragment,按钮都应保留在 Dialog 底部.
                • From a FragmentActivity show a dialog when clicking an Action Button in the Action Bar
                • DialogFragment - A Dialog without title
                • TabHost - Tabs at the top of the dialog
                • ViewPager with FragmentPagerAdapter - Swipable, which content is connected to the Tabs
                • 2-3 Dialog Buttons (different subclasses of the Dialog, different buttons) - Are not supposed to be in one of the ViewPager's Fragment, meaning the same buttons should remain at the bottom of the Dialog regardless of what Fragment the ViewPager is showing.


                IllegalStateException: Fragment does not have a view
                


                • android.support.v4 包用于必要的类
                • 调用 getChildFragmentManager() 而不是 getSupportedFragmentManager()
                • 实施了此链接建议的第 10 条帖子 https://code.google.com/p/android/issues/detail?id=42601.我将代码直接复制/粘贴到我的两个 Fragment 类中,其中 ViewPager 应该显示,加上 DialogFragment 类.
                • 在我的自定义 DialogFragment 中,我首先尝试覆盖 onCreateView,然后是 onCreateDialog,然后同时覆盖两者.我必须运行所有这些,但结果出乎意料.
                  • 只有 onCreateView:无法访问 AlertDialog.Builder 来创建所需的按钮,除了 Dialog 的结果很好.
                  • onCreateDialog:如上所示的错误信息.我仍然认为这种方法与我想要实现的目标一样接近.
                  • onCreateViewonCreateDialog:在 onCreateView 中扩充 Dialog 布局,并将 Dialog 按钮添加到 AlertDialog.BuilderonCreateDialog 中的代码>.这显示了对话框,但从 AlertDialog.Builder 添加的按钮不可见.另外,当点击 EditText 字段时,键盘没有显示出来.
                  • Using the android.support.v4 package for necessary classes
                  • Calling getChildFragmentManager() instead of getSupportedFragmentManager()
                  • Implemented what post #10 suggested from this link https://code.google.com/p/android/issues/detail?id=42601. I copy/paste the code directly into my two Fragment classes, which the ViewPager is suppose to be showing, plus the DialogFragment class.
                  • In my custom DialogFragment I first tried to override onCreateView, then onCreateDialog and then both at the same time. All of which I got to run but with unexpected results.
                    • Only onCreateView: Can't reach the AlertDialog.Builder to create the needed buttons, other than that the Dialog's results were great.
                    • Only onCreateDialog: the error message shown above. I still imagine this method to be as close as I've gotten to what I want to achieve.
                    • Both onCreateView and onCreateDialog: Inflated the Dialog layout in onCreateView and added the Dialog buttons to the AlertDialog.Builder in onCreateDialog. This displayed the dialog, but the added buttons from the AlertDialog.Builder were not visable. Plus the keyboard didn't show up when clicking on a EditText field.


                    大部分来自 Android 2.2 + ViewPager 和 Fragments 使用 TabHost 的教程.ActivityFragment 的代码位于 DialogFragment 中.但是,我将其 ViewPager 替换为来自此答案的源代码的修改版本 https://stackoverflow.com/a/18167273/2375978.这是为了能够在高度上 wrap_content.

                    Most come from Tutorial to implement the use of TabHost in Android 2.2 + ViewPager and Fragments. The code of the ActivityFragment is instead in a DialogFragment. However I replaced its ViewPager with a modified one from the source code from this answer https://stackoverflow.com/a/18167273/2375978. This was to be able to wrap_content on height.

                    我的项目中的错误代码在 DialogFragmentonCreateDialog 方法中,我相信.

                    The faulty code in my project is in DialogFragment's onCreateDialog method, I believe.

                    @Override
                    public Dialog onCreateDialog(Bundle savedInstanceState) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), AlertDialog.THEME_HOLO_DARK);
                        LayoutInflater inflater = getActivity().getLayoutInflater();
                        view = inflater.inflate(R.layout.dialog_test, null);
                        addActionButtons(builder, view);    
                        builder.setView(view);
                    
                        mViewPager = (WrapContentHeightViewPager) view.findViewById(R.id.viewpager);
                    
                        initialiseTabHost();
                    
                        List<Fragment> fragments = getFragments();
                    
                        pageAdapter = new DialogPageAdapter(getChildFragmentManager(), fragments);
                        mViewPager.setAdapter(pageAdapter);
                        mViewPager.setOnPageChangeListener(this);
                    
                        Dialog dialog = builder.create();
                        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                        dialog.show();
                    
                        return dialog;
                    }
                    


                    FATAL EXCEPTION: main
                    java.lang.IllegalStateException: Fragment does not have a view
                        at android.support.v4.app.Fragment$1.findViewById(Fragment.java:1425)
                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:901)
                        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
                        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
                        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
                        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
                        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                        at android.support.v4.view.ViewPager.populate(ViewPager.java:1011)
                        at android.support.v4.view.ViewPager.populate(ViewPager.java:880)
                        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1374)
                        at my.app.package.name.WrapContentHeightViewPager.onMeasure(WrapContentHeightViewPager.java:31)
                        at android.view.View.measure(View.java:15481)
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
                        at android.view.View.measure(View.java:15481)
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                        at android.view.View.measure(View.java:15481)
                        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
                        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
                        at android.view.View.measure(View.java:15481)
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                        at android.view.View.measure(View.java:15481)
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                        at android.view.View.measure(View.java:15481)
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
                        at android.view.View.measure(View.java:15481)
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                        at android.view.View.measure(View.java:15481)
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                        at android.view.View.measure(View.java:15481)
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5059)
                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
                        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2377)
                        at android.view.View.measure(View.java:15481)
                        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1982)
                        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1200)
                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1398)
                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1118)
                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4525)
                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
                        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
                        at android.view.Choreographer.doFrame(Choreographer.java:525)
                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
                        at android.os.Handler.handleCallback(Handler.java:615)
                        at android.os.Handler.dispatchMessage(Handler.java:92)
                        at android.os.Looper.loop(Looper.java:137)
                        at android.app.ActivityThread.main(ActivityThread.java:4946)
                        at java.lang.reflect.Method.invokeNative(Native Method)
                        at java.lang.reflect.Method.invoke(Method.jav
                    


                    • 我未能成功尝试 https 中提到的其他可能的解决方案://code.google.com/p/android/issues/detail?id=42601,在帖子 #2 和 #13 中提到,因为我不明白如何以及在哪里可以在我的代码中使用它(我想我和写 #18 的人在同一条船上).
                    • I have unsuccessfully been able to try the other possible solution mentioned in https://code.google.com/p/android/issues/detail?id=42601, mentioned in post #2 and #13, because I haven't understood how and where I can use it in my code (I guess I'm in the same boat as the person who wrote #18).

                    推荐答案

                    我想我只是遇到了同样的问题,并通过查看 DialogFragment.

                    I think I just ran into this same problem and learned a few things by looking at the source for DialogFragment.

                    看起来即使重写 onCreateDialog(...) 是创建自定义对话框的有效方法,它也会导致 DialogFragment 具有空视图,就像错误消息中所说的那样.在大多数情况下,这很好 - DialogFragment 不需要 View 来显示 Dialog,但如果你想进一步嵌套 Fragment(就像你做的那样),这不会飞.

                    It looks like even though overriding onCreateDialog(...) is a valid way to create a custom dialog, it will result in the DialogFragment having a null View, just like the error message says. In most cases this is fine - the DialogFragment doesn't need a View to show a Dialog, but if you want to nest fragments further (like you do), this won't fly.

                    考虑到您想与 AlertDialog.Builder 进行交互,我认为确实没有完美的解决方案,但您有几个选择:

                    Considering that you want to interact with an AlertDialog.Builder, there is really no perfect solution that I can see, but you've got a few options:

                    1. 在对话框中创建按钮作为视图的一部分(不使用 AlertDialog.Builder).您可以通过覆盖 onCreateView 而不是 onCreateDialog 来做到这一点.您应该能够通过将按钮放在自己的片段中来获得您提到的功能.我们在我的演出中做了类似的事情,我非常喜欢这种方法.
                    2. 实现您自己的类型,该类型继承自 Fragment 并以各种方式反映 DialogFragment,但允许您需要的内容除外.这不应该太可怕,因为 DialogFragment 只有 ~400 并且被大量评论.可能会很有趣.
                    3. 使用常规 PagerAdapter 而不是 FragmentPagerAdapter.这样,您的 DialogFragment 没有 View 就无关紧要了.
                    1. Create the buttons in your dialog as part of the View (not using AlertDialog.Builder). You do this by overriding onCreateView instead of onCreateDialog. You should be able to get the functionality you mention by putting the buttons in their own fragment. We do something similar at my gig, and I very much prefer this method.
                    2. Implement your own type that inherits from Fragment and that mirrors the DialogFragment in every way except allowing what you need. This shouldn't be too scary as DialogFragment is only ~400 and is heavily commented. Could be fun.
                    3. Use a regular PagerAdapter instead of a FragmentPagerAdapter. This way it won't matter that your DialogFragment doesn't have a View.

                    这篇关于DialogFragment 中的 ViewPager - IllegalStateException:片段没有视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:仅在第二次导航到 Fragment 时导致 java.IllegalStateException 错误,No Activ 下一篇:如何自动滑动android View Pager

                相关文章

              • <tfoot id='AzkRa'></tfoot>
                <i id='AzkRa'><tr id='AzkRa'><dt id='AzkRa'><q id='AzkRa'><span id='AzkRa'><b id='AzkRa'><form id='AzkRa'><ins id='AzkRa'></ins><ul id='AzkRa'></ul><sub id='AzkRa'></sub></form><legend id='AzkRa'></legend><bdo id='AzkRa'><pre id='AzkRa'><center id='AzkRa'></center></pre></bdo></b><th id='AzkRa'></th></span></q></dt></tr></i><div id='AzkRa'><tfoot id='AzkRa'></tfoot><dl id='AzkRa'><fieldset id='AzkRa'></fieldset></dl></div>

                    • <bdo id='AzkRa'></bdo><ul id='AzkRa'></ul>

                    <small id='AzkRa'></small><noframes id='AzkRa'>

                    <legend id='AzkRa'><style id='AzkRa'><dir id='AzkRa'><q id='AzkRa'></q></dir></style></legend>