我正在尝试使用listview创建一个dialogfragment,并使用此问题中接受的答案来执行此操作
How to display an existing ListFragment in a DialogFragment
但是当我尝试打开片段对话框并且应用崩溃时,我收到了Error inflating class fragment
以下是dialog_fragment_with_list_fragment
布局
<?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>
并且它不是导致它失败的androidxmlparsingactivity片段,我已经尝试了其他片段并且它们也无法正常工作
下面是我的对话框片段类
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;
}
}
这就是我如何调用dialogfragment
public void onClick(View v) {
BodyDialogue dialogFragment = BodyDialogue.newInstance(1);
dialogFragment .setRetainInstance(true);
dialogFragment .show(getFragmentManager(), "bodydialogue");
}
这是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)
答案 0 :(得分:5)
但是当我尝试打开时,我收到一个错误的类错误 片段对话框和应用程序崩溃
这种情况正在发生,因为您将BodyDialogue
片段的视图用作已包含其他片段(通过fragment
标记)的布局。这将失败,因为不允许嵌套片段从xml布局中膨胀,因为the guide for the nested fragments已经提到:
注意:当布局包含&lt;时,您不能将布局扩展为片段。片断&gt ;.只有在动态添加到片段时才支持嵌套片段。
因此,如果您想在AndroidXMLParsingActivity
对话框片段中嵌入BodyDialogue
(片段命名错误btw),请使用{{1在同一onCreateView
回调中的代码中执行此操作}}
答案 1 :(得分:0)
我使用这个简单易用的
解决了我的问题请参阅此回答
https://stackoverflow.com/a/14966061/963591
我的代码使用上面的回答
@Override
public void onClick(View v) {
// TODO Auto-generated method
// stub
FragmentTransaction ft2 = getFragmentManager().beginTransaction();
ft2.remove(getFragmentManager().findFragmentByTag("one"));
ft2.commit();
dialog.dismiss();
}
我的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" />