What I want to achieve
- 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.
The problem
IllegalStateException: Fragment does not have a view
What I have tried/done so far
- 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.
- Only
Source code
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.
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;
}
Stack trace LogCat log
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
Also...
- 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).
Thanks to @Tommy Visic for writing a really good description it worked.
Posting the code which worked for me.
I have removed the
Dialog
building code from theonCreateDialog
method infact removedonCreateDialog
method and the dialog view which I have been implementing in theDialog's
custom view I have included it as aView
in theonCreateView
method and all the things started working.Faced one more problem with this implementation is:
When a
Activity
hasToolbar/ActionBar
then it is also displayed into theDialogFragment
to avoid that what is to be done is: ImplementonViewCreated
method ofFragment
and add below codeThis will remove the
Toolbar
from theDialogFragment
andActivity
will be displayed as it is.Cheers
Thanks @Tommy
Regards Zeus
If you implement
onCreateDialog
to useAlertDialog
, you will bump intoIllegalStateException: Fragment does not have a view
when accessinggetChildFragmentManager
or something equivalent.To solve this issue, implement both
onCreateDialog
andonCreateView
, whereonCreateView
return the view inflated inonCreateDialog
.https://code.luasoftware.com/tutorials/android/android-alertdialog-in-dialogfragment-fragment-does-not-have-a-view/
I think I just ran into this same problem and learned a few things by looking at the source for 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.
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:
Use the
instead of onCreateDialog(Bundle savedInstanceState). Don't create an alert dialog, use the inflater provided by the method, and build your view. It works for me.
Best regards!