0
充氣類片段

我試圖用一個列表視圖創建它的內部dialogfragment和我以前接受的答案,從這個問題做錯誤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

是的,因爲如果我只是叫它來代替一個片段,它看起來很好。就像我說的那樣,它與該片段無關,我在其他片段上測試過它們,並且它們也不工作,我添加了logcat,我認爲您可能會找到解決問題的方法 –

+0

請不要在您的問題前加上字詞Android或任何其他標籤的標題,底部的標籤綽綽有餘。 – Luksprog

回答

5

但我得到一個錯誤充氣類片段,當我嘗試打開 片段對話框和應用程序崩潰

發生這種情況,因爲你作爲BodyDialogue片段的視圖中使用的佈局已經包含了另一個片段(通過fragment標籤)。這將失敗,因爲嵌套片段不允許從一個XML佈局充氣,如the guide for the nested fragments已經提到:

注意:不能膨脹的佈局成片段時佈局包括<片段>。只有在動態添加到片段時才支持嵌套片段。

所以,如果你想(順便說一句可怕命名爲片段)嵌入AndroidXMLParsingActivityBodyDialogue對話片段,然後做在代碼中使用getChildFragmentManager()相同onCreateView回調。

+0

你有沒有這樣的例子? –

+0

@CamConnor看看https://gist.github.com/luksprog/6265896如果你還沒有自己做。 – Luksprog

+1

+1「嵌套片段的指南已經提到過了......似乎你是唯一一個讀過它的人:)直到我讀完這篇文章之前,整夜都在與xmls + nestedfrags戰鬥。 – rupps

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我的代碼