【问题标题】:Binary XML file line #9: Binary XML file line #9: Error inflating class fragment二进制 XML 文件第 9 行:二进制 XML 文件第 9 行:膨胀类片段时出错
【发布时间】:2017-10-08 20:06:45
【问题描述】:

当我尝试向我的班级添加片段时,我不断收到此错误,我不确定如何解决这个问题,我很确定我正确添加了片段,但是当我尝试运行它时,错误不断出现并使我的应用程序崩溃,不知道如何要解决这个问题?

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout_default"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ericvuu.whatsfordinner.RecipeActivity">

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.ericvuu.whatsfordinner.RecipeActivity"/>

    <Button
        android:id="@+id/recipe1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginBottom="484dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_constraintBottom_creator="1"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        app:layout_constraintHorizontal_bias="0.0" />

    <Button
        android:id="@+id/recipe2"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimaryDark"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recipe1"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />

    <Button
        android:id="@+id/recipe3"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginTop="-11dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/recipe2"
        tools:layout_constraintLeft_creator="1"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintTop_creator="1" />
</android.support.constraint.ConstraintLayout>

Java 文件

 package com.example.ericvuu.whatsfordinner;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class RecipeActivity extends AppCompatActivity {

        public Button recipeText;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_recipe);

             recipeText = (Button) findViewById(R.id.recipe1);


                try {
                    String message;
                    FileInputStream fis = openFileInput("RecipeText");
                    InputStreamReader isr = new InputStreamReader(fis);
                    BufferedReader br = new BufferedReader(isr);
                    StringBuffer sb = new StringBuffer();
                    while ((message = br.readLine()) != null) {
                        sb.append(message + "\n");
                    }
                    recipeText.setText(sb.toString());

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }

        public void onClick(View v) {

        }

    }

和我试图将片段放入的景观 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <fragment
        android:id="@+id/layout_default"
        android:name="com.example.ericvuu.whatsfordinner.RecipeActivity"
        android:layout_width="214dp"
        android:layout_height="match_parent"
        android:layout_weight="0.08" />

</LinearLayout>

【问题讨论】:

  • RecipeActivity 是一个Activity。它不是Fragment。您不能将其视为Fragment。此外,每当您收到InflateException 时,您都需要在堆栈跟踪中进一步查看实际原因。 InflateException 本身并没有告诉你太多。

标签: java android xml android-fragments


【解决方案1】:

您正在为垂直和横向模式添加 xml 片段。因此,一旦您启动应用程序,它就会崩溃,因为您已经在活动中添加了片段。我建议最好尝试使用容器添加片段。

例如:- 将此行放入您的 xml 中

   <LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

而不是将直接片段添加到 xml 中。

并尝试以编程方式添加您的片段。

 FragmentTransaction fragmentTransaction = 
 getFragmentManager().beginTransaction();
 .......
 ......

还要检查 RecipeActivity 是不是片段的活动。此代码仅适用于片段

【讨论】:

  • 我要把它放在横向 xml 中吗?
【解决方案2】:

作为@MikeM。在评论中告诉你,你不能在片段标签内为android:name 属性使用 Activity。

Fragments documentation 说:

中的android:name属性指定Fragment 在布局中实例化的类。

因此,您不能使用以下内容:

<fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.ericvuu.whatsfordinner.RecipeActivity"/>

您需要将您的RecipeActivity 活动转换为片段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 2022-01-17
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多