Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - ListFragment inside DialogFragment(AlertDialog): Fragment does not have a view

I'm trying to create a DialogFragment with a ListFragment nested inside of it. I'm using the nested fragment that was added in the 4.2.2 I'm using it with support library so my api version shouldn't matter (for the record, my os version is also 4.2.2).

The listfragment that I'm trying to add only shows strings right now because I'm trying to add it, after I will do that, the list will have images and such.. So please any ideas of passing the strings list to the dialog isn't good for me, My list will be more complicated.. Also, I'm trying to reuse this fragment for tablet mode. The list always should be shown in that mode. So the reuse is important.

My problem is when I'm trying to add the listfragment to the FrameLayout of the dialog, it cannot be found:

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.Fragment.performStart(Fragment.java:1481) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941) 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$1.run(FragmentManager.java:429) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) 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)


ADDED:

Maybe this new information is important, The DialogFragment is shown within a SupportMapFragment. When I try just to show the ListFragment inside the SupportMapFragment, it works. Also, when I'm trying to show just the DialogFragment inside the SupportMapFragment - it works.

Just when I try to display the ListFragment within the DialogFragment it fails. So it's look like this: FragmentActivity -> SupportMapFragment -> DialogFragment -> ListFragment.

The showing of the DialogFragment from the SupportMapFragment looks like this:

// DialogFragment.show() will take care of adding the fragment
// in a transaction.  We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
SherlockDialogFragment prev =(SherlockDialogFragment)getChildFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
    ft.remove(prev);
}
ft.addToBackStack(null);

// Create and show the dialog.
SherlockDialogFragment newFragment = new CategoriesDialogFragment();
newFragment.show(ft, "dialog");


Please Help :-)


My DialogFragment class is this:

public class CategoriesDialogFragment extends SherlockDialogFragment {

// {{ Events

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.fragment_categories_dialog_fragment, null, false);

    Dialog diag = new AlertDialog.Builder(getSherlockActivity())
            .setTitle("Hello")
            .setCancelable(false)
            .setView(dialogView)
            .setPositiveButton("ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {                          

                    }
                }
            )
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                    }
                }
            )
            .create();

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    SherlockDialogFragment prev = (SherlockDialogFragment)getChildFragmentManager().findFragmentByTag("fragment_list_categories");
    if (prev != null) {
        transaction.remove(prev);
    }

    SherlockListFragment listCategoriesFragment = new CategoriesListFragment();
    transaction.add(R.id.fragment_list_categories, listCategoriesFragment, "fragment_list_categories").commit();

    return diag;
}
}

My dialog layout is this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="sjrdflksjlksjfkldsjflkjsdflksd"/>
<FrameLayout
    android:id="@+id/fragment_list_categories"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" />
</LinearLayout>

And my ListFragment class is this:

public class CategoriesListFragment extends SherlockListFragment {
public static final String[] TITLES = 
    {
            "Henry IV (1)",   
            "Henry V",
            "Henry VIII",       
            "Richard II",
            "Richard III",
            "Merchant of Venice",  
            "Othello",
            "King Lear"
    };

// {{ Events

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    Log.w("ListFragment", "insideeeeeeeeeeeeeeeeeeeee");
    super.onActivityCreated(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(getSherlockActivity(),
            android.R.layout.simple_list_item_1, TITLES));

    // setHasOptionsMenu(true);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO: Kazaz - implement category pressed
    super.onListItemClick(l, v, position, id);
}

// }}
}
like image 429
Or Kazaz Avatar asked Apr 11 '13 15:04

Or Kazaz


People also ask

How do you show DialogFragment?

Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle. A fragment that displays a dialog window, floating on top of its activity's window.

How do you pass arguments to DialogFragment?

you can set your args. class IntervModifFragment : DialogFragment(), ModContract. View { companion object { fun newInstance( plom:String,type:String,position: Int):IntervModifFragment { val fragment =IntervModifFragment() val args = Bundle() args. putString( "1",plom) args.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


1 Answers

Not sure if this will work for you, but maybe try to have that code nested in onCreateView instead. I had a similar problem and it worked for me.

Essentially remove what you have in onCreateDialog and maybe something like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_categories_dialog_fragment, container, false);

    // And so on...
    getDialog().setTitle("");

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    SherlockListFragment listCategoriesFragment = new CategoriesListFragment();
    transaction.add(R.id.fragment_list_categories, listCategoriesFragment, "fragment_list_categories").commit();

    return view;
}
like image 82
user2565845 Avatar answered Nov 14 '22 23:11

user2565845