【问题标题】:Error when loading fragment from activity从活动加载片段时出错
【发布时间】:2017-11-08 16:34:22
【问题描述】:

我是 android studio 新手,在从 Activity 启动片段时遇到了一些麻烦。我使用从这里开始的代码来显示片段,它一直工作到现在。我正在使用一个名为“DecoView”的第三方库,它需要扩展片段,所有代码都在“LineDetails”中(添加所有代码太长了)。有人认出下面的这个错误吗?

提前谢谢你!

行详情 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="@+id/fragment_place"
android:layout_height="match_parent">

  <android.support.v4.view.ViewPager
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/pager"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />
  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_alignParentTop="true"
      android:layout_alignParentStart="true">
    <com.hookedonplay.decoviewlib.DecoView
        android:layout_width="250dp"
        android:layout_height="200dp"
        android:id="@+id/dynamicArcView"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="5dp">

    </com.hookedonplay.decoviewlib.DecoView>

    <EditText
        android:id="@+id/actual"
        android:inputType="number"
        android:layout_width="150dp"
        android:layout_height="46dp"
        android:background="@drawable/border"
        android:gravity="center"
        android:hint="Enter Actual"
        android:text=""
        android:textColor="#000000"
        android:textColorHint="#000000"
        android:textSize="15sp"
        android:layout_marginStart="26dp"
        android:layout_alignBottom="@+id/textPercentage"
        android:layout_alignStart="@+id/dynamicArcView" />

    <Button
        android:id="@+id/actual_btn"
        android:layout_width="68dp"
        android:layout_height="50dp"
        android:background="@drawable/actualbutton"
        android:shadowColor="#A8A8A8"
        android:text="+"
        android:textColor="#FFFFFF"
        android:layout_above="@+id/textViewProgress"
        android:layout_toEndOf="@+id/actual"
        android:layout_marginLeft="12dp"/>

    <TextView
        android:id="@+id/textViewProgress"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:layout_marginTop="15dp"
        android:layout_below="@+id/actual"
        android:layout_alignEnd="@+id/actual_btn"
        android:layout_marginEnd="14dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text=""
        android:textSize="40sp"
        android:id="@+id/textPercentage"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="90dp"
        />

    <TextView
        android:id="@+id/textViewLineType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="86dp"
        android:text=""
        android:textSize="40dp"
        android:textAlignment="center"
        android:textStyle="bold"
        android:textColor="#f15623"
        android:layout_alignTop="@+id/dynamicArcView"
        android:layout_toEndOf="@+id/dynamicArcView" />
    <Button
        android:id="@+id/buttonViewDetails"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@mipmap/info"
        android:layout_marginLeft="250dp"
        android:onClick="verifyButton"

        android:layout_alignParentBottom="true"
        android:layout_marginBottom="14dp" />

  </RelativeLayout>


  </RelativeLayout>

【问题讨论】:

  • 你在膨胀你的 xml 时有一个错误你可以添加line_details.xml*
  • 它不会让我添加所有的 xml
  • 已添加行详细信息 xml

标签: java android android-fragments fragment android-fragmentactivity


【解决方案1】:
<fragment
android:id="@+id/fragment_name"
class="com.almac.tracker.LineDetails"
android:layout_width="match_parent"
android:layout_height="match_parent" />

"xmlns:android="http://schemas.android.com/apk/res/android"
不见了

使用下面的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <fragment
        android:id="@+id/fragment_name"
        class="com.almac.tracker.LineDetails"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

【讨论】:

  • 我已经在顶部添加了,但还是谢谢,还有其他想法吗?
  • 发布您的 R.layout.line_details 文件
  • 现在添加了
  • 这正是我在 fragment.xml 中所拥有的
【解决方案2】:

尝试添加

    tools:context="com.yourpackagehere.FragmentThatIsInflatingTheXml">

到您的第一个布局,即相对布局。请注意,您必须将“yourpackagehere”更改为您的包名称,将“FragmentThatIsInflatingTheXml”更改为实际 Fragment 的名称

【讨论】:

  • java.lang.RuntimeException:无法启动活动 ComponentInfo{com.almac.tracker/com.almac.tracker.Actforfragmentname}:android.view.InflateException:二进制 XML 文件行 #0:二进制 XML文件第 0 行:膨胀类片段时出错
  • 如果对您有帮助,我仍然会收到此错误
  • 当您尝试在 xml 组件上使用非原始类型而没有在您的应用程序上正确设置它时,会出现此错误,只有一个问题,一旦我多年来遇到此错误,我只是将其更改为等效的原始类型,因此我无法告诉您是否需要在 Gradle 或 AndroidManifest 文件中进行设置,只需尝试搜索“设置新的 xml 组件 Android”之类的内容
【解决方案3】:

你说 DecoView 扩展了一个片段,所以应该像这样添加它:

<fragment
  android:id="@+id/fragment_id"                  
  android:name="com.hookedonplay.decoviewlib.DecoView"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

【讨论】:

  • 添加了该行但仍然崩溃
  • 你能发布崩溃的堆栈跟踪吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
相关资源
最近更新 更多