如何在DialogFragment

时间:2016-01-05 04:37:52

标签: android android-fragments android-preferences

我一直在尝试在dialogFragment中显示PreferenceFragment,但我不确定如何......

我尝试将preferencefragment放在布局

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent">
   <fragment
    android:id="@+id/settings_fragment"
    android:name="com.example.test.Fragments.SettingsFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
   </LinearLayout>

然后在onCreateView

中使用它
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    super.onCreate(savedInstanceState);
    val view = inflater!!.inflate(R.layout.dialog_preference, container, false)
    return view
}

在我的主要活动中,我这样称呼它:

   override fun onClick(view: View) {
    val id = view.id
        when (id) {
            R.id.btnSettings -> {
                val dialog = SettingsDialog();
                dialog.onCancel(object : DialogInterface {
                 override fun cancel() {
                     setTheme()
                 }

                 override fun dismiss() {

                 }
                })
                dialog.show(fragmentManager, null);
            }
        }
    }

我第一次打电话,它会出现,但第二次尝试。它崩溃了.. 还有另一种在对话框中显示PreferenceFragment的方法吗? 有人可以让我知道怎么做吗? 谢谢

这是我得到的错误信息

  

android.view.InflateException:二进制XML文件行#5:错误膨胀类片段       在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)       在android.view.LayoutInflater.rInflate(LayoutInflater.java:806)       在android.view.LayoutInflater.inflate(LayoutInflater.java:504)       在android.view.LayoutInflater.inflate(LayoutInflater.java:414)       在com.example.test.Dialog.SettingsDialog.onCreateView(SettingsDialog.kt:20)       在android.app.Fragment.performCreateView(Fragment.java:2053)       在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)       在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)       在android.app.BackStackRecord.run(BackStackRecord.java:834)       在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)       在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:447)       在android.os.Handler.handleCallback(Handler.java:739)       在android.os.Handler.dispatchMessage(Handler.java:95)       在android.os.Looper.loop(Looper.java:135)       在android.app.ActivityThread.main(ActivityThread.java:5254)       at java.lang.reflect.Method.invoke(Native Method)       在java.lang.reflect.Method.invoke(Method.java:372)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)    引起:java.lang.IllegalArgumentException:二进制XML文件行#5:复制id 0x7f0e0080,标记null或父id 0xffffffff与com.example.test.Fragments.SettingsFragment的另一个片段       在android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2120)       在android.app.Activity.onCreateView(Activity.java:5328)       在android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:34)       在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)       在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)       在android.view.LayoutInflater.rInflate(LayoutInflater.java:806)       在android.view.LayoutInflater.inflate(LayoutInflater.java:504)       在android.view.LayoutInflater.inflate(LayoutInflater.java:414)       在com.example.test.Dialog.SettingsDialog.onCreateView(SettingsDialog.kt:20)       在android.app.Fragment.performCreateView(Fragment.java:2053)       在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)       在android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)       在android.app.BackStackRecord.run(BackStackRecord.java:834)       在android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)       在android.app.FragmentManagerImpl $ 1.run(FragmentManager.java:447)       在android.os.Handler.handleCallback(Handler.java:739)       在android.os.Handler.dispatchMessage(Handler.java:95)       在android.os.Looper.loop(Looper.java:135)       在android.app.ActivityThread.main(ActivityThread.java:5254)       at java.lang.reflect.Method.invoke(Native Method)       在java.lang.reflect.Method.invoke(Method.java:372)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:2)

我在另一篇文章中找到了答案: Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

所以当我把它添加到我的onDestroyView

     override fun onDestroyView() {
    super.onDestroyView()
    val frag = fragmentManager.findFragmentById(R.id.settings_fragment) as SettingsFragment?;
    if (frag != null)
        fragmentManager.beginTransaction().remove(frag).commit();
}

一切正常。如果有人能想出更好的方法来创建偏好对话,请告诉我