【问题标题】:Error inflating Fragment in Dialog the second time第二次在对话框中膨胀片段时出错
【发布时间】:2011-10-23 21:42:21
【问题描述】:

我在一个 Activity 中有以下代码,它为包含片段的布局启动一个对话框。

...
case R.id.pick_resource:
        dialog = new Dialog(this);
        dialog.setContentView(R.layout.resource_picker);
        dialog.setCancelable(true);
        dialog.setTitle("Pick a resource");
        dialog.show();

这在应用程序启动后第一次运行良好,但是当对话框退出并稍后再次调用时,我得到了这个堆栈跟踪:

08-10 10:47:33.990: ERROR/AndroidRuntime(26521): FATAL EXCEPTION: main
        android.view.InflateException: Binary XML file line #7: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:688)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:724)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:391)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:347)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:224)
        at android.app.Dialog.setContentView(Dialog.java:449)
        at org.rhq.pocket.StartActivity.onOptionsItemSelected(StartActivity.java:118)
        at android.app.Activity.onMenuItemSelected(Activity.java:2390)
        at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:852)
        at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:956)
        at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:174)
        at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:85)
        at android.view.View.performClick(View.java:3100)
        at android.view.View$PerformClick.run(View.java:11644)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:126)
        at android.app.ActivityThread.main(ActivityThread.java:3997)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:491)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f090007, tag null, or parent id 0xffffffff with another fragment for org.rhq.pocket.ResourcePickerFragement
        at android.app.Activity.onCreateView(Activity.java:4089)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:664)
        ... 24 more

任何想法,可能导致此异常的原因是什么?我必须以某种方式卸载片段吗?

【问题讨论】:

  • 你在哪里声明了“dialog”变量?检查你是否将“dialog”声明为类变量。
  • 添加更多代码有助于我们理解问题
  • @Jainal 在活动中,但是当我放置 Dialog dialog = new Dialog(this) 时这并没有改变。

标签: android dialog fragment


【解决方案1】:

布局中的片段是否有 android:id 属性?

我怀疑这是因为每次布局膨胀时都会实例化片段,第一次没有使用 ID,但第二次 FragmentManager 仍然认为您的 Fragment 是活动的,因此 ID 被认为是重复的.

尝试从片段中删除 android:id 属性(如果存在),或者添加一个占位符布局,例如 framelayout 并使用片段事务在每次创建对话框时动态添加片段。

【讨论】:

  • 哇,我已经搜索了 6 个小时,以了解为什么在尝试从容器中添加/删除片段时出现重复 ID 错误。你是一个生命和理智的拯救者:D 太棒了! ^^
  • 删除片段的 ID/标签有效,但假设您不需要查找对它的引用。以编程方式添加片段(如您所述)是一种选择。当对话框被关闭时(例如,在 OnDismissListener 中)以编程方式将其删除是另一种方法。这种方法对我很有效。
  • 这对我不起作用。如果我删除 id,我会得到一个异常:Binary XML 文件第 103 行:必须指定唯一的 android:id、android:tag,或者有一个带有 com.google.android.gms.maps id 的父级。地图片段
  • 知道他为什么会这样想,当父母被完全摧毁的时候?
  • 对于必须指定unique android:id, android:tag, or have a parent with an id for ...的用户,您可以将<fragment>包装在例如<RelativeLayout>中,并为RelativeLayout指定id并从片段中删除任何id。
【解决方案2】:

我也遇到过类似的问题

我很容易就解决了

关闭对话框时删除所有片段

这是我的对话

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="310dp"
    android:layout_height="260dp" >

     <fragment
        android:layout_width="310dp"
        android:layout_height="260dp"
        android:tag="one"
        class="com.android.mushrooms.CookingProgressFragment" />

    <fragment
        android:layout_width="310dp"
        android:layout_height="260dp"
        android:tag = "two"
        class="com.android.mushrooms.CookingInfoFragment" />

</RelativeLayout>

当你删除一个对话框时会发生这种情况

dialog.findViewById(R.id.cifButtonClose)
                .setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        mDragController.endDrag();

                        FragmentTransaction ft2 = getSupportFragmentManager()
                                .beginTransaction();

                        ft2.remove( getSupportFragmentManager()
                                .findFragmentByTag("one"));
                        ft2.remove( getSupportFragmentManager()
                                .findFragmentByTag("two"));
                        ft2.commit();
                        dialog.dismiss();
                    }
                });

【讨论】:

    【解决方案3】:

    如下尝试

    public class ABDCD{
        private Dialog dialog = null;
        private View viewHoldingDialog = null;
        ----------------------------
    
    case R.id.pick_resource:
        dialog = new Dialog(this,R.style.Theme_Dialog_Translucent);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        viewHoldingDialog = (ViewGroup) layoutInflater.inflate(
                R.layout.resource_picker, null);
        dialogLayoutParams = new LayoutParams(android.view.WindowManager.LayoutParams.WRAP_CONTENT, android.view.WindowManager.LayoutParams.WRAP_CONTENT);
        dialog.addContentView(viewHoldingDialog,
    
    
        dialogLayoutParams);
    

    }

    【讨论】:

      【解决方案4】:

      也许这可以帮助某人:

      我使用 Эвансгелист Evansgelist 的代码,但是对于具有 MapFragment 布局的 DialogFragment,代码必须是 onDestroy 块:

      @Override
      public void onDestroy(){
          super.onDestroy();
          FragmentTransaction ft2 = getActivity().getFragmentManager()
                  .beginTransaction();
      
          ft2.remove( getFragmentManager()
                  .findFragmentById(R.id.map_map));
          ft2.commit();
      }
      

      这是我的布局:

      <?xml version="1.0" encoding="utf-8"?>
      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
               xmlns:tools="http://schemas.android.com/tools"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               tools:context="com.example"
      >
      <fragment
            android:id="@+id/map_map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.google.android.gms.maps.MapFragment"/>
      </FrameLayout>
      

      【讨论】:

      • 这是正确的答案。我在onPause 中调用dismiss() 然后删除onDismiss 中的片段
      • 非常感谢@M D P。您的解决方案是唯一对我有用的解决方案。
      【解决方案5】:
      On android.view.inflateexception: binary xml file line #8: error inflating class fragment error :
      

      尝试从片段中删除 android:id 属性(如果存在)。 并在关闭对话框中添加以下代码:它适用于我

      activity.getFragmentManager().beginTransaction().remove(activity.getFragmentManager().findFragmentById(R.id.mMap_location)).commit();
      

      【讨论】:

        【解决方案6】:

        另一个可以帮助其他人的答案:我遇到了类似的问题,但我没有销毁 onDestroy 中的片段,而是将其移至 onPause 以使其工作:

        @Override
        protected void onPause() {
            if(adFragment != null){
                childFragmentManager.beginTransaction().remove(adFragment).commitAllowingStateLoss();
            }
            super.onPause();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-19
          • 2013-05-28
          相关资源
          最近更新 更多