【问题标题】:Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Must specify unique android:id, android:tag, or have a parent with an id for原因:java.lang.IllegalArgumentException:二进制 XML 文件第 9 行:必须指定唯一的 android:id、android:tag,或者有一个带有 id 的父级
【发布时间】:2018-07-09 16:53:49
【问题描述】:

我正在尝试从head first Android book 学习 Android,这是它的代码之一,但我不明白为什么它会给我这个奇怪的错误?由于我是 Android 新手,因此无法解决问题并将其发布在这里。不知道这个数据够不够?详细活动是一个活动,它的.xml 文件上有一个片段,并指向一个片段活动,其布局包括两个文本视图。

DetailActivity.java:

package com.example.m.workoutapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class DetailActivity extends AppCompatActivity {

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

activity_detail.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.m.workoutapp.DetailActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.m.workoutapp.WorkoutDetailFragment" />

</android.support.constraint.ConstraintLayout>

错误发生在这一行:android:name="com.example.m.workoutapp.WorkoutDetailFragment"

WorkoutDetailFragment.java:

package com.example.m.workoutapp;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class WorkoutDetailFragment extends Fragment {


    public WorkoutDetailFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_workout_detail, container, false);
    }

}

fragment_workout_detail:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.m.workoutapp.WorkoutDetailFragment">

    <TextView
        android:id="@+id/text_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="workout title"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

    <TextView
        android:id="@+id/text_desciption"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="workout description"
        />

</LinearLayout>

完整的错误信息:

01-30 21:38:45.386 25393-25393/com.example.m.workoutapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.m.workoutapp, PID: 25393
                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.m.workoutapp/com.example.m.workoutapp.DetailActivity}: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                           Caused by: android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment
                                                                           Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment
                                                                           Caused by: java.lang.IllegalArgumentException: Binary XML file line #10: Must specify unique android:id, android:tag, or have a parent with an id for com.example.m.workoutapp.WorkoutDetailFragment
                                                                              at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3385)
                                                                              at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                              at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378)
                                                                              at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
                                                                              at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                                                                              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
                                                                              at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                              at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                              at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                                              at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)
                                                                              at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                              at com.example.m.workoutapp.DetailActivity.onCreate(DetailActivity.java:11)
                                                                              at android.app.Activity.performCreate(Activity.java:6679)
                                                                              at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                              at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:154)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

【问题讨论】:

  • 你能在这里粘贴整个错误堆栈跟踪吗?它在标题中被切断

标签: java android xml android-fragments error-handling


【解决方案1】:

错误在您的activity_detail.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.m.workoutapp.DetailActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.m.workoutapp.WorkoutDetailFragment" />

</android.support.constraint.ConstraintLayout>

当您使用&lt;fragment&gt; 标签时,系统会自动创建您的WorkoutDetailFragment 的实例并将其添加到活动的FragmentManager。为此,系统需要某种方式向片段管理器识别您的片段。您有三个选择:

  1. android:id 属性添加到&lt;fragment&gt; 标记的父级(ConstraintLayout

  2. android:id 属性添加到&lt;fragment&gt; 标记本身

  3. android:tag 属性添加到&lt;fragment&gt; 标记本身

任何这些都将允许片段管理器识别和跟踪片段,这将解决您的崩溃问题。

您使用什么tagid 甚至都不重要(尽管选择描述性名称总是好的)。只要您不在 Activity 布局中的其他位置重复使用 id 或标签,就可以了。

【讨论】:

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