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);
}
// }}
}