【问题标题】:java.lang.RuntimeException: Unable to start activity ComponentInfo(class: MainActivity)java.lang.RuntimeException:无法启动活动 ComponentInfo(类:MainActivity)
【发布时间】:2019-08-15 03:25:30
【问题描述】:

我在模拟器上启动我的应用程序时遇到此异常 java.lang.RuntimeException: Unable to start Activity ComponentInfo(class: MainActivity): android.view.InflateException: Binary XML file line #31: Binary XML file line #19: Error inflating class fragment

在这一行

ViewFlipper list = view.findViewById(R.id.newList);

我受影响的布局

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:paddingHorizontal="16dp"
        android:paddingVertical="8dp">

        <TextView
            android:id="@+id/test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/test_up"
            android:textAppearance="?android:textAppearanceMedium"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" /> 

<LinearLayout
    android:layout_width="400dp"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/limitPageSize">
    android:id="@+id/newlist"


</LinearLayout>

</android.support.constraint.ConstraintLayout>

【问题讨论】:

  • 发布您的 logcat 消息,其中显示“由...引起”。这是 MainActivity 使用 setContentView(...) 加载的相同布局吗?
  • android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 在 android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
  • 你能发布更多代码吗?
  • 添加以上代码

标签: android android-fragments fragment


【解决方案1】:

我认为您的片段需要一个容器,但发布的代码量使这有点不清楚。

https://developer.android.com/guide/components/fragments

您可以像这样在视图中托管片段(来自文档):

<?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">
    <fragment android:name="com.example.news.ArticleListFragment"
            android:id="@+id/list"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <fragment android:name="com.example.news.ArticleReaderFragment"
            android:id="@+id/viewer"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>

或者你可以只使用 FrameLayout。

<LinearLayout
        android:id="@+id/newList"
        android:layout_width="400dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/limitPageSize">

      <FrameLayout
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    </LinearLayout>

【讨论】:

    【解决方案2】:

    在 .xml 中替换那个

    android.support.constraint.ConstraintLayout
    

    androidx.constraintlayout.widget.
    

    并在 app.gradle 中添加依赖项

    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    

    【讨论】:

      猜你喜欢
      • 2014-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-10
      • 2014-02-07
      相关资源
      最近更新 更多