IllegalStateException: DialogFragment can not be attached to a container view - (in ActionBarActivity)
T

4

12

In my Activity (that extends android.support.v7.app.ActionBarActivity) I have a fragment that is normally include into my views like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     >
     <fragment 
      android:name="com.fragments.SomeFragment"
      android:id="@+id/fragment_id"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />
</FrameLayout>

That works well.

But I've decided to reuse this fragment as a dialog, so I extended it by android.support.v4.app.DialogFragment (from supported library).

Without any other changes I get the exception

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ExampleActivity}: java.lang.IllegalStateException: DialogFragment can not be attached to a container view
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

    Caused by: java.lang.IllegalStateException: DialogFragment can not be attached to a container view
at android.support.v4.app.DialogFragment.onActivityCreated(DialogFragment.java:364)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1486)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:552)
at com.*.RoboActionBarActivity.onStart(RoboActionBarActivity.java:58)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1164)
at android.app.Activity.performStart(Activity.java:5114)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)
... 11 more

How can I resolve this problem or use my standard fragment as a dialog?

My Activity:

public class SomeActivity extends RoboActionBarActivity{

    private SomeFragment someFragment;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_some);

        this.someFragment = (SomeFragment) getSupportFragmentManager().findFragmentById(
            R.id.fragment_id);
    }
}

RoboActionBarActivity is a ActionBarActivity that implements RoboContext (from RoboGuice) to enable injection.

public class RoboActionBarActivity extends ActionBarActivity implements RoboContext {
private EventManager eventManager;
private final HashMap<Key<?>, Object> scopedObjects = new HashMap<Key<?>, Object>();

@Inject
private ContentViewListener ignored; // BUG find a better place to put this

@Override
protected void onCreate(final Bundle savedInstanceState) {
    final RoboInjector injector = RoboGuice.getInjector(this);
    this.eventManager = injector.getInstance(EventManager.class);
    injector.injectMembersWithoutViews(this);
    super.onCreate(savedInstanceState);
    this.eventManager.fire(new OnCreateEvent(savedInstanceState));
}

@Override
protected void onRestart() {
    super.onRestart();
    this.eventManager.fire(new OnRestartEvent());
}

@Override
protected void onStart() {
    super.onStart();
    this.eventManager.fire(new OnStartEvent());
}

@Override
protected void onResume() {
    super.onResume();
    this.eventManager.fire(new OnResumeEvent());
}

@Override
protected void onPause() {
    super.onPause();
    this.eventManager.fire(new OnPauseEvent());
}

@Override
protected void onNewIntent(final Intent intent) {
    super.onNewIntent(intent);
    this.eventManager.fire(new OnNewIntentEvent());
}

@Override
protected void onStop() {
    try {
        this.eventManager.fire(new OnStopEvent());
    } finally {
        super.onStop();
    }
}

@Override
protected void onDestroy() {
    try {
        this.eventManager.fire(new OnDestroyEvent());
    } finally {
        try {
            RoboGuice.destroyInjector(this);
        } finally {
            super.onDestroy();
        }
    }
}

@Override
public void onConfigurationChanged(final Configuration newConfig) {
    final Configuration currentConfig = getResources().getConfiguration();
    super.onConfigurationChanged(newConfig);
    this.eventManager.fire(new OnConfigurationChangedEvent(currentConfig, newConfig));
}

@Override
public void onContentChanged() {
    super.onContentChanged();
    RoboGuice.getInjector(this).injectViewMembers(this);
    this.eventManager.fire(new OnContentChangedEvent());
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    this.eventManager.fire(new OnActivityResultEvent(requestCode, resultCode, data));
}

@Override
public Map<Key<?>, Object> getScopedObjectMap() {
    return this.scopedObjects;
}
}
Tope answered 20/11, 2013 at 11:7 Comment(5)
Can you show the relevant part of the code?Agnew
Can you be more specific by what do you mean with relevant parts?Tope
Just what you added :)Agnew
I have the same issue I want to use a DialogFragment in an activity but use only the Fragment piece of it, don't want to rewrite code for this one off I have. Is there a way to work around thisStockjobber
We have encountered the same issue in our project. DialogFragment can not be attached to a container view .We had a LinearLayout in the layout inflated named @+id/root, the solution was to rename the element.Juniper
G
2

You can correct this issue if you invoke setShowsDialog(false) somewhere in onCreate() inside of your DialogFragment.

But you must invoke setShowsDialog(true) when you want to use this fragment as a dialog.

Grapnel answered 3/1, 2016 at 18:38 Comment(1)
If you use this, when and where should setShowDialog(true) be called if I want to show the fragment as a dialog agian?Procumbent
A
2

For me it happened because I called DialogFragment#show on the same fragment multiple times.

I fixed this by creating new fragment instance before every DialogFragment#show call.

Accouter answered 25/8, 2020 at 13:54 Comment(1)
But why does it crash when the same instance is used?Tights
L
1

I know this was asked a while ago but the way I got around this was to create my fragment dynamically rather than in xml. There's probably a better solution but this got it working for me.

public class MyActivity {

    private MyFragment mMyFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            mMyFragment = new MyFragment();
            if (mMyFragment != null) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction()
                    .add(android.R.id.content, mMyFragment)
                    .commit();
            }
        }
    }
}
Lapidate answered 10/4, 2014 at 16:4 Comment(0)
H
0
public class myDialogFragment extends DialogFragment {
    View mView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
        if(mView == null ) {
            // Inflate the layout for this fragment
            mView = inflater.inflate(R.layout.fragment_dialog, container, false);
        }
        return mView;
    }

    @Override
    public void dismiss(FragmentManager manager, String tag)
    {
        super.dismiss();
        mView = null;
    }
}
Head answered 24/1, 2020 at 16:25 Comment(1)
Can you explain your answer please? why does the same view does not work?Tights

© 2022 - 2024 — McMap. All rights reserved.