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

时间:2015-10-11 06:03:48

标签: android android-fragments illegalstateexception inflate-exception

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

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_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >

    <fragment
        android:id="@+id/monday_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="center"
    android:background="@color/light_background">

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        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_width="match_parent"
           android:layout_height="match_parent"
           android:text="As of now, no homework due this day"
           android:padding="30dp" 
           android:textSize="16sp"/>

</LinearLayout>

以下是它抛出的错误的日志,我已经尝试了这个postpost,但都没有解决问题。

  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之后才开始发生的。也许这与它有关?可能所有片段引用都搞砸了?

7 个答案:

答案 0 :(得分:4)

您的日志指向第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);

答案 1 :(得分:4)

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

答案 2 :(得分:3)

我认为这段代码很可疑:

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

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

我知道这些ID令人困惑。只有像findViewById()这样的方法才能指向UI ID。

答案 3 :(得分:3)

请做以下两件事:

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

  2. monday_fragment.xml重命名android:id="@+id/monday_fragment"android:id="@+id/monday_fragment1"

    请参阅以下代码:

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

答案 4 :(得分:2)

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

如果您将软件包导入为jar文件,那么这是一个不同的案例/问题。

答案 5 :(得分:1)

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

答案 6 :(得分:0)

通过xml绑定片段通常会带来负面的微妙后果。动态地这样做。

将monday_fragment.xml更改为:

"frameworks": {
"dotnet5.4": {     
},
"net451": {
  "EntityFramework.Commands": "7.0.0-rc1-final",
  "frameworkAssemblies": { "System.Runtime": "4.0.10.0" }
}
}

并更改此代码块:

<?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_width="match_parent"
    android:layout_height="match_parent"
    >

</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();

修改让我们尝试使用DialogFragment

请创建课程DayHomeworkDialogFragment:

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

而不是这个

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(); 

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