Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
}
}
like image 740
salcosand Avatar asked Nov 20 '13 11:11

salcosand


3 Answers

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.

like image 158
ivan Avatar answered Nov 19 '22 22:11

ivan


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.

like image 38
user2229336 Avatar answered Nov 19 '22 22:11

user2229336


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();
            }
        }
    }
}
like image 22
wchristiansen Avatar answered Nov 20 '22 00:11

wchristiansen