I am trying to create a dialogfragment with a listview inside of it and I used the accepted answer from this question to do it
How to display an existing ListFragment in a DialogFragment
But I am getting an Error inflating class fragment
when I try to open the fragment dialog and the app crashes
Below is the dialog_fragment_with_list_fragment
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding = "10dp"
class="com.OptimusApps.stayhealthy.AndroidXMLParsingActivity" />
</LinearLayout>
and it is not the androidxmlparsingactivity fragment that is causing it to fail, I have tried it with other fragments and they did not work either
Below is my dialog fragment class
public class BodyDialogue extends DialogFragment {
int mNum;
/**
* Create a new instance of MyDialogFragment, providing "num"
* as an argument.
*/
static BodyDialogue newInstance(int num) {
BodyDialogue f = new BodyDialogue();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putInt("num", num);
f.setArguments(args);
return f;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments().getInt("num");
// Pick a style based on the num.
int style = DialogFragment.STYLE_NORMAL, theme = 0;
switch ((mNum-1)%6) {
case 1: style = DialogFragment.STYLE_NO_TITLE; break;
case 2: style = DialogFragment.STYLE_NO_FRAME; break;
case 3: style = DialogFragment.STYLE_NO_INPUT; break;
case 4: style = DialogFragment.STYLE_NORMAL; break;
case 5: style = DialogFragment.STYLE_NORMAL; break;
case 6: style = DialogFragment.STYLE_NO_TITLE; break;
case 7: style = DialogFragment.STYLE_NO_FRAME; break;
case 8: style = DialogFragment.STYLE_NORMAL; break;
}
switch ((mNum-1)%6) {
case 4: theme = android.R.style.Theme_Holo; break;
case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break;
case 6: theme = android.R.style.Theme_Holo_Light; break;
case 7: theme = android.R.style.Theme_Holo_Light_Panel; break;
case 8: theme = android.R.style.Theme_Holo_Light; break;
}
setStyle(style, theme);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_fragment_with_list_fragment, null);
return view;
}
}
and this is how i call the dialogfragment
public void onClick(View v) {
BodyDialogue dialogFragment = BodyDialogue.newInstance(1);
dialogFragment .setRetainInstance(true);
dialogFragment .show(getFragmentManager(), "bodydialogue");
}
this was the cause in the logcat
08-17 19:43:15.702: E/AndroidRuntime(3605): Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f0a0031, tag null, or parent id 0x0 with another fragment for com.OptimusApps.stayhealthy.AndroidXMLParsingActivity
08-17 19:43:15.702: E/AndroidRuntime(3605): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:285)
But I am getting an Error inflating class fragment when I try to open the fragment dialog and the app crashes
This is happening because you use as the BodyDialogue
fragment's view a layout which already includes another fragment(through the fragment
tag). This will fail as nested fragments aren't allowed to be inflated from a xml layout, as the guide for the nested fragments already mentions:
Note: You cannot inflate a layout into a fragment when that layout includes a < fragment>. Nested fragments are only supported when added to a fragment dynamically.
So if you want to embed the AndroidXMLParsingActivity
(awful naming btw for a fragment) in the BodyDialogue
dialog fragment then do it in code in the same onCreateView
callback using getChildFragmentManager()
.
I solved my problem using this simple and easy
Please refer to this answer
https://stackoverflow.com/a/14966061/963591
My code using above answer
@Override
public void onClick(View v) {
// TODO Auto-generated method
// stub
FragmentTransaction ft2 = getFragmentManager().beginTransaction();
ft2.remove(getFragmentManager().findFragmentByTag("one"));
ft2.commit();
dialog.dismiss();
}
My xml
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="220dip"
android:tag="one"
android:layout_below="@+id/txtTargetStreet_hint"
android:layout_margin="20dip" />
来源:https://stackoverflow.com/questions/18294269/error-inflating-class-fragment-in-dialogfragment