片段对话框中的片段膨胀引发错误“片段未创建视图”

Posted

技术标签:

【中文标题】片段对话框中的片段膨胀引发错误“片段未创建视图”【英文标题】:Fragment inflated in Fragment Dialog throws error "Fragment did not create a view" 【发布时间】:2016-01-08 19:23:46 【问题描述】:

用户单击一个按钮会弹出一个片段对话框,该对话框会像这样膨胀片段:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        View view = getActivity().getLayoutInflater().inflate(
                R.layout.monday_fragment, null);
        builder.setView(view).setTitle("Homework Due Monday")
                .setNegativeButton("Dismiss", null);

        AlertDialog dialog = builder.create();
        dialog.show();

这是指定布局的 xml (monday_fragment.xml),其中对要膨胀的片段的引用位于:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_
    android:layout_
    tools:context=".MainActivity"
    >

    <fragment
        android:id="@+id/monday_fragment"
        android:layout_
        android:layout_
        android:name="com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

片段对话框中的片段然后在其 OnCreateView 中膨胀自己的布局:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) 

        swipe = inflater.inflate(R.layout.day_homework_fragment, container,
                false);

        lView = (ListView) swipe.findViewById(R.id.listView1);

        progress = (ProgressBar) swipe.findViewById(R.id.progress);

        lView.setVisibility(View.GONE);

        return swipe;

    

这是片段中膨胀的“day_homework_fragment.xml”布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_
    android:layout_
    android:orientation="vertical"
    android:layout_gravity="center"
    android:background="@color/light_background">

    <ProgressBar
        android:id="@+id/progress"
        android:layout_
        android:layout_
        android:layout_gravity="center" />

    <ListView
        android:id="@+id/listView1"
        android:layout_
        android:layout_
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
         android:background="#ffffff"
         android:divider="@null"
        android:dividerHeight="0dp"
         >
    </ListView>

           <TextView 
           android:id="@+id/emptyView"
           android:layout_
           android:layout_
           android:text="As of now, no homework due this day"
           android:padding="30dp" 
           android:textSize="16sp"/>

</LinearLayout>

这是它引发的错误日志,我已经尝试过 post 和 post 都没有解决问题。

  10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime: FATAL EXCEPTION: main
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime: Process: com.bernard.beaconportal.activities, PID: 786
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:668)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(<Xposed>)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.bernard.beaconportal.activities.schedule.view.MondayView.showDialog(MondayView.java:232)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.bernard.beaconportal.activities.schedule.view.MondayView$1.onClick(MondayView.java:174)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.View.performClick(View.java:4780)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19867)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5338)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:  Caused by: java.lang.IllegalStateException: Fragment com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment did not create a view.
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2273)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:278)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.app.BaseFragmentActivityDonut.onCreateView(BaseFragmentActivityDonut.java:44)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:78)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.callActivityOnCreateView(AppCompatDelegateImplV7.java:842)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV11.callActivityOnCreateView(AppCompatDelegateImplV11.java:34)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:830)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.invokeOriginalMethodNative(Native Method) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.handleHookedMethod(XposedBridge.java:668) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(<Xposed>) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.bernard.beaconportal.activities.schedule.view.MondayView.showDialog(MondayView.java:232) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.bernard.beaconportal.activities.schedule.view.MondayView$1.onClick(MondayView.java:174) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.View.performClick(View.java:4780) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19867) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5338) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:917) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 
    10-11 01:50:40.657 786-786/com.bernard.beaconportal.activities E/AndroidRuntime:     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115) 

编辑:我刚刚意识到这只是在我从 Eclipse 迁移到 Android Studio 之后才开始发生的。也许这与它有关?可能所有的片段引用都搞砸了?

【问题讨论】:

您是否为您的片段致电onCreateViewonCreate 将标记显示到您的day_homework_fragment.xml 片段 @Tauqir 我确实为我的片段调用了 oncreateview,它膨胀了“day_homework_fragment” @selbie 添加了“day_homework_fragment.xml”的标记 从您调用 AlertDialog 的位置 【参考方案1】:

您不能将片段放在标准 AlertDialog 中。您必须创建一个DialogFragment 实现来将片段添加到您的对话框中。原因很简单,因为标准片段会尝试将自己附加到相应活动的根窗口,而AlertDialog 在根窗口上有一个单独的窗口时情况并非如此。 AlertDialog 不能包含 DialogFragment 甚至是 Fragment。尝试将您的 AlertDialog 代码更改为 DialogFragment 实现。

【讨论】:

我认为你有一个观点,因此我赞成你的回答。在我看来,您的建议可能是最好的。但是作者开发人员可能不想更改太多代码。在这种情况下,这可能是一个错误。【参考方案2】:

您的日志指向第 10 行这一事实意味着此处的 xml 声明是一个问题,正如片段在第 10 行所说的那样:

 android:name="com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment"

我会在这里仔细检查这个引用,看看它是否指向您为 Fragment 保存 java 类的正确位置。

另外,检查您的 MondayFragment 类,您正在扩展正确的 Fragment 类。也就是说,如果你使用的是支持库,你需要扩展支持库的 Fragment 类,而不是普通的 Fragment 类。

如果一切都失败了,您可以随时以编程方式添加片段:http://developer.android.com/training/basics/fragments/fragment-ui.html

编辑:我看到您目前正在使用以下代码膨胀您的 xml:

View view = getActivity().getLayoutInflater().inflate(
                R.layout.monday_fragment, null);

既然你是在一个片段中膨胀它,你能不能只说:

View view = inflater.inflate(R.layout.monday_fragment, null,
                false);

【讨论】:

我很确定我有正确的包名,因为即使 IDE 也能识别出我指向的是正确的片段。我也在为我的 Fragment 类使用支持库。 在 Android Studio 上尝试“File | Invalidate Caches/Restart”。【参考方案3】:

我认为这段代码是可疑的:

 View view = getActivity().getLayoutInflater().inflate(
            R.layout.monday_fragment, null);

inflate的第一个参数应该是xml文件名而不是片段ID。也许您想要 xml R.layout.day_homework_fragment

我知道这些 ID 令人困惑。只有 findViewById() 之类的方法指向 UI ID。

【讨论】:

哦,对不起,我应该在我的帖子中更清楚。 “monday_fragment.xml”也是包含片段引用的布局的名称。我会在我的帖子中更清楚地说明这一点。 澄清 monday_fragment 不是指片段 ID,而是指 xml 布局【参考方案4】:

做以下两件事:

    检查com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment是否是正确的包名(路径)

    在您的monday_fragment.xml 中将android:id="@+id/monday_fragment" 重命名为android:id="@+id/monday_fragment1"

    见以下代码:

    <Fragment
       android:id="@+id/monday_fragment1"
       android:layout_
       android:layout_
       android:name="com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment"
       android:layout_centerHorizontal="true"/>
    

【讨论】:

我很确定我的包名是正确的,因为即使 IDE 也能识别出我指向的是正确的片段。此外,我刚刚尝试了#2,似乎 ID 与任何内容都没有冲突,因为我遇到了同样的错误。这是一个奇怪的错误,我不知道还能尝试什么。 如果您使用的是Android Studio,则删除[app模块]/build/下的所有子文件夹并重新编译项目 删除构建文件夹并重新编译后仍然没有变化。现在我想这只是在我从 eclipse 迁移到 Android Studio 之后才开始发生的,也许这与它有关。【参考方案5】:

我相信你必须自己调试包com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment。迁移到 Android Studio 后,UI 引用可能不再有效。一种简单的调试方法是删除OnCreateView() 中的代码。

如果您将包作为 jar 文件导入,则情况不同/问题。

【讨论】:

@I'm_With_Stupid,所以...如果您删除 OnCreateView 中的代码,应用还会崩溃吗?【参考方案6】:

你为什么不使用对话框片段而不是 Alertdialog?它对于在活动范围内调用片段很重要。对话框片段将具有所有生命周期方法,这将有助于创建任何后续片段。

【讨论】:

【参考方案7】:

通过 xml 绑定片段通常会带来负面的微妙后果。动态执行此操作。

改变你的 monday_fragment.xml 像:

<?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:id="@+id/container"
    android:layout_
    android:layout_
    >

</FrameLayout>

并更改这段代码:

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

View view = getActivity().getLayoutInflater().inflate(
        R.layout.monday_fragment, null);

builder.setView(view).setTitle("Homework Due Monday")
        .setNegativeButton("Dismiss", null);

MondayFragment mondayFragment = new MondayFragment();
getFragmentManager().beginTransaction()
                    .replace(R.id.container, mondayFragment)
                    .commit();

AlertDialog dialog = builder.create();
dialog.show();

编辑:如果失败尝试更改

getFragmentManager().beginTransaction()
        .replace(R.id.container, mondayFragment)
        .commit();

getParentFragment().getChildFragmentManager().beginTransaction()
            .replace(R.id.container, mondayFragment)
            .commit();

编辑让我们试试DialogFragment

请创建类 DayHomeworkDialogFragment:

public class DayHomeworkDialogFragment extends DialogFragment 

    public DayHomeworkDialogFragment() 
    

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 


        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View swipe = inflater.inflate(R.layout.day_homework_fragment, null);

        lView = (ListView) swipe.findViewById(R.id.listView1);
        progress = (ProgressBar) swipe.findViewById(R.id.progress);
        lView.setVisibility(View.GONE);



        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity())
                .setView(swipe)
                .setTitle("Homework Due Monday")
                .setPositiveButton("Add", new DialogInterface.OnClickListener() 
                    @Override
                    public void onClick(DialogInterface dialog, int which) 

                    
                )
                .setNegativeButton("Dismiss",
                        new DialogInterface.OnClickListener() 
                            public void onClick(DialogInterface dialog, int whichButton) 
                                dialog.dismiss();
                            
                        
                );

        return alertDialogBuilder.create();
    

而不是这个

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        View view = getActivity().getLayoutInflater().inflate(
                R.layout.monday_fragment, null);
        builder.setView(view).setTitle("Homework Due Monday")
                .setNegativeButton("Dismiss", null);

        AlertDialog dialog = builder.create();
        dialog.show(); 

使用这个:

FragmentManager fragmentManager = getFragmentManager();
DayHomeworkDialogFragment dayHomeworkDialogFragment = new DayHomeworkDialogFragment();
dayHomeworkDialogFragment.show(fragmentManager, "my_day_homework_fragment");

如果失败,请尝试使用 getChildFragmentManager() 而不是 getFragmentManager()

【讨论】:

无论我尝试什么,我都会得到这个error。它声称 R.id.container 不存在。我什至尝试在使用 framelayout.getId() 引用它之前启动 FrameLayout。似乎没有任何效果。 甚至 getChildFragmentManager() 都没有帮助? 所以我最终做的是使“com.bernard.beaconportal.activities.schedule.daydialogfragments.MondayFragment”成为 DialogFragment 而不是 Fragment。我只是希望我们能通过最初的错误弄清楚为什么会这样。 @I'm_With_Stupid 我对最初的错误有一些想法,但我需要一些时间来阅读源代码并测试一些假设。当我完成后,我会告诉你问题的原因

以上是关于片段对话框中的片段膨胀引发错误“片段未创建视图”的主要内容,如果未能解决你的问题,请参考以下文章

布局膨胀错误片段

谷歌地图的“错误膨胀类片段”

android.view.InflateException Binary xml 文件第 11 行:错误膨胀类片段

膨胀类片段时出错

多片段层次结构中的菜单膨胀问题

android.view.inflateexception 二进制 xml 文件第 7 行错误膨胀类片段