ViewPager在DialogFragment - IllegalStateException异常:片段不具有的图 [英] ViewPager in DialogFragment - IllegalStateException: Fragment does not have a view
问题描述
- 从一个
FragmentActivity
显示一个对话框,点击操作栏的操作按钮时, -
DialogFragment
- 一个对话框没有标题 -
TabHost
- 选项卡在对话框的顶部 -
ViewPager
与FragmentPagerAdapter
- Swipable,哪些内容连接到标签 - 在2-3对话框按钮(对话框的不同亚类,不同的按钮) - 难道不应该在一个
ViewPager
的片段
,意思是一样的按钮应保持在该对话框的底部,无论是什么片段
的ViewPager
是显示
- From a
FragmentActivity
show a dialog when clicking an Action Button in the Action Bar DialogFragment
- A Dialog without titleTabHost
- Tabs at the top of the dialogViewPager
withFragmentPagerAdapter
- 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
'sFragment
, meaning the same buttons should remain at the bottom of the Dialog regardless of whatFragment
theViewPager
is showing.
IllegalStateException: Fragment does not have a view
- 使用进行必要类 的
- 电话
getChildFragmentManager()
而不是getSupportedFragmentManager()
- 在实现什么岗位#10从这个链接提示 <一个href="https://$c$c.google.com/p/android/issues/detail?id=42601">https://$c$c.google.com/p/android/issues/detail?id=42601.我复制/粘贴code直接进入我的两个
片段
类,其中ViewPager
的假设是显示,加上DialogFragment
类。 - 在我的自定义
DialogFragment
我第一次尝试重写onCreateView
,然后onCreateDialog
,然后都在同一时间。所有这一切,我得走了,但有意想不到的效果。- 仅
onCreateView
:无法到达AlertDialog.Builder
以创建所需要的按钮,比其他对话的结果是巨大的。 - 仅
onCreateDialog
:上面显示的错误消息。我还想象这种方法,因为我已经得到了我想要实现以尽可能接近。 - 在两个
onCreateView
和onCreateDialog
:充气的对话框布局onCreateView
并添加对话框按钮在AlertDialog.Builder
在onCreateDialog
。这显示该对话框,但从AlertDialog.Builder
添加按钮并不visable。加上键盘上的的EditText
字段点击时没有露面。
- Using the
android.support.v4
package for necessary classes - Calling
getChildFragmentManager()
instead ofgetSupportedFragmentManager()
- 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 theViewPager
is suppose to be showing, plus theDialogFragment
class. - In my custom
DialogFragment
I first tried to overrideonCreateView
, thenonCreateDialog
and then both at the same time. All of which I got to run but with unexpected results.- Only
onCreateView
: Can't reach theAlertDialog.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
andonCreateDialog
: Inflated the Dialog layout inonCreateView
and added the Dialog buttons to theAlertDialog.Builder
inonCreateDialog
. This displayed the dialog, but the added buttons from theAlertDialog.Builder
were not visable. Plus the keyboard didn't show up when clicking on aEditText
field.
大多数来自 <一个href="http://stackoverflow.com/questions/17687717/tutorial-to-implement-the-use-of-tabhost-in-android-2-2-viewpager-and-fragment">Tutorial实现用TabHost在安卓2.2 + ViewPager和片段 。的code中的
ActivityFragment
是不是在DialogFragment
。但是我从这个答案更换了ViewPager从源头code一个经过修改的 <一个href="https://stackoverflow.com/a/18167273/2375978">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 aDialogFragment
. 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 towrap_content
on height.有故障的code在我的项目是
DialogFragment
的onCreateDialog
的方法,我相信。The faulty code in my project is in
DialogFragment
'sonCreateDialog
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
- 我失败已经能够尝试<一提到的其他可能的解决方案href="https://$c$c.google.com/p/android/issues/detail?id=42601">https://$c$c.google.com/p/android/issues/detail?id=42601,后#2,#13所提到的,因为我不明白如何以及在何处,我可以在我的code使用它(我猜我在同一条船上,是谁写的#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并不需要一个视图来显示一个对话框,但是如果你要嵌套片段进一步(像你这样做),这不会飞
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:
- 创建在对话框的按钮作为视图(不使用AlertDialog.Builder)的一部分。您可以通过覆盖onCreateDialog的的onCreateView的代替。您应该能够通过将按钮在它们自己的片段让你提到的功能。我们做同样的事情在我的演出,我非常preFER此方法。
- 实施自己的类型继承自片段和反映在各方面的DialogFragment除了让你所需要的。这不应该是太可怕,因为DialogFragment只〜400,并大量注释。可能是有趣的。
- 使用常规PagerAdapter而不是FragmentPagerAdapter。这样,它不会不管你的DialogFragment没有一个视图。
- 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.
- 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.
- Use a regular PagerAdapter instead of a FragmentPagerAdapter. This way it won't matter that your DialogFragment doesn't have a View.
这篇关于ViewPager在DialogFragment - IllegalStateException异常:片段不具有的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Only
- 仅
android.support.v4
包