首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >二进制XML文件第9行:二进制XML文件第9行:膨胀类片段时出错

二进制XML文件第9行:二进制XML文件第9行:膨胀类片段时出错
EN

Stack Overflow用户
提问于 2017-10-09 04:06:45
回答 2查看 1.2K关注 0票数 0

当我试图添加一个片段到我的类时,我一直收到这个错误,我不确定如何修复这个错误,我非常确定我添加的片段是正确的,但是当我试图运行它的时候,这个错误一直出现并使我的应用崩溃,你知道如何修复它吗?

XML文件

代码语言:javascript
复制
<?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文件

代码语言:javascript
复制
 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文件im triyng将片段放入其中。

代码语言:javascript
复制
<?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>
EN

回答 2

Stack Overflow用户

发布于 2017-10-09 04:24:57

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

例如:-将这一行放入xml

代码语言:javascript
复制
   <LinearLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

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

代码语言:javascript
复制
 FragmentTransaction fragmentTransaction = 
 getFragmentManager().beginTransaction();
 .......
 ......

还要检查RecipeActivity是一个activity而不是片段。此代码仅适用于片段

票数 0
EN

Stack Overflow用户

发布于 2017-10-09 17:47:32

作为@MikeM。在注释中,您不能在片段标记内使用android:name属性的活动。

Fragments documentation说:

中的android:

属性指定要在布局中实例化的片段类。

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

代码语言:javascript
复制
<fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.ericvuu.whatsfordinner.RecipeActivity"/>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46635519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档