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

我想要实现的

>从FragmentActivity中单击操作栏中的“操作”按钮时将显示一个对话框
> DialogFragment – 没有标题的对话框
> TabHost – 对话框顶部的选项卡
> ViewPager with FragmentPagerAdapter – Swipable,哪些内容连接到Tabs
> 2-3对话框按钮(对话框的不同子类,不同的按钮) – 不应该在ViewPager的片段之一中,这意味着相同的按钮应该保留在对话框的底部,而不管ViewPager显示什么片段。

问题

IllegalStateException: Fragment does not have a view

我到目前为止已经尝试过/做了什么

>将android.support.v4包用于必要的类
>调用getChildFragmentManager()而不是getSupportedFragmentManager()
>实现了从这个链接https://code.google.com/p/android/issues/detail?id=42601建议的帖子#10。我将代码直接复制/粘贴到我的两个Fragment类中,ViewPager假定要显示,还有DialogFragment类。
>在我的自定义DialogFragment中,我首先尝试重写onCreateView,然后再次onCreateDialog,然后同时进行。所有这一切我都要跑,但意想不到的结果。

>只有onCreateView:无法到达AlertDialog.Builder创建所需的按钮,除了对话框的结果是伟大的。
>只有onCreateDialog:上面显示的错误信息。我仍然想象这种方法与我想要实现的一样接近。
> onCreateView和onCreateDialog:在onCreateView中填充Dialog布局,并将对话框按钮添加到onCreateDialog中的AlertDialog.Builder中。这显示对话框,但是AlertDialog.Builder中添加的按钮是不可见的。此外,当单击EditText字段时,键盘没有显示。

源代码

大部分来自Tutorial to implement the use of TabHost in Android 2.2 + ViewPager and Fragments. ActivityFragment的代码代替在DialogFragment中。然而,我从这个答案https://stackoverflow.com/a/18167273/2375978的源代码中更换了一个修改的ViewPager。这是能够在高度上wrap_content。

我的项目中的错误代码是DialogFragment的onCreateDialog方法,我相信。

@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;
}

堆栈跟踪LogCat日志

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的人)。

我想我刚刚遇到同样的问题,并通过查看DialogFragment的来源学到了一些东西。

看起来尽管覆盖onCreateDialog(…)是一种创建自定义对话框的有效方法,但它将导致DialogFragment具有null视图,就像错误消息所示。在大多数情况下,这是很好的 – DialogFragment不需要View来显示对话框,但如果你想进一步嵌套碎片(就像你这样做),这不会飞。

考虑到你想和AlertDialog.Builder交互,真的没有完美的解决方案,我可以看到,但你有几个选择:

>在视图中创建对话框中的按钮(不使用AlertDialog.Builder)。您可以通过覆盖onCreateView而不是onCreateDialog来执行此操作。您应该可以通过将按钮放在自己的片段中来获得您提到的功能。我在演唱会上做类似的事情,我更喜欢这种方法。
>实现您自己的类型继承Fragment,并以各种方式镜像DialogFragment,除了允许您需要的。这不应该太恐怖,因为DialogFragment只有〜400,并且被大量评论。可以很有趣。
>使用常规PagerAdapter而不是FragmentPagerAdapter。这样就不需要你的DialogFragment没有View。

翻译自:https://stackoverflow.com/questions/20303865/viewpager-in-dialogfragment-illegalstateexception-fragment-does-not-have-a-vi

转载注明原文:android – DialogFragment中的ViewPager – IllegalStateException:片段没有视图