简体   繁体   English

引起原因:java.lang.IllegalArgumentException:二进制XML文件第9行:必须指定唯一的android:id,android:tag或具有ID的父对象

[英]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

I am trying to learn Android from head first Android book , and this is one of it's codes, but I don't why it gives me this strange error? 我正在尝试从head first Android书籍中学习Android,这是其中的代码之一,但是我不为什么它会给我这个奇怪的错误? As I am new in Android couldn't solve the problem and posted it here. 由于我是Android新手,因此无法解决问题并将其发布在此处。 I don't know if this data enough or not? 我不知道这些数据是否足够? Detail activity is an activity that has a fragment on it's .xml file and points to a fragment activity with a layout includes two text view on it. 细节活动是在.xml文件中具有片段的活动,并指向一个片段活动,该活动的布局包括两个文本视图。

DetailActivity.java: 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: 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>

The error happens in this line: android:name="com.example.m.workoutapp.WorkoutDetailFragment" 该行中发生错误: android:name="com.example.m.workoutapp.WorkoutDetailFragment"

WorkoutDetailFragment.java: 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: 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>

Full error message: 完整的错误消息:

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)

The error is in your activity_detail.xml file: 错误出现在您的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> 

When you use the <fragment> tag, the system will automatically create an instance of your WorkoutDetailFragment and add it to the activity's FragmentManager . 当使用<fragment>标记时,系统将自动创建WorkoutDetailFragment的实例并将其添加到活动的FragmentManager To do this, the system needs some way to identify your fragment to the fragment manager. 为此,系统需要某种方式向片段管理器标识您的片段。 You have three choices: 您有三种选择:

  1. Add an android:id attribute to the <fragment> tag's parent (the ConstraintLayout ) android:id属性添加到<fragment>标记的父项( ConstraintLayout

  2. Add an android:id attribute to the <fragment> tag itself <fragment>标签本身添加android:id属性

  3. Add an android:tag attribute to the <fragment> tag itself <fragment>标签本身添加android:tag属性

Any of these will allow the fragment manager to identify and track the fragment, which will solve your crash. 这些中的任何一个都将允许片段管理器识别和跟踪片段,这将解决您的崩溃问题。

It doesn't even really matter what tag or id you use, either (though it's always good to pick descriptive names). 甚至使用什么tagid都不是很重要(尽管选择描述性名称总是很不错的)。 As long as you don't re-use the id or tag somewhere else in your activity's layout, it will be fine. 只要您不在活动布局中的其他地方重复使用ID或标签,就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 引起:java.lang.IllegalArgumentException: Entity must have id - Caused by: java.lang.IllegalArgumentException: Entity must has id Android:java.lang.IllegalArgumentException:错误的对话线程 ID:15555215554 - Android: java.lang.IllegalArgumentException: Bad conversation thread id: 15555215554 Android SQLite java.lang.IllegalArgumentException:列&#39;_id&#39;不存在 - Android SQLite java.lang.IllegalArgumentException: column '_id' does not exist 给定的 id 不能是 null;。 嵌套异常是 java.lang.IllegalArgumentException - The given id must not be null!; nested exception is java.lang.IllegalArgumentException Android-java.lang.IllegalArgumentException:找不到ID为xxxxx(android:id / content)的片段的视图 - Android - java.lang.IllegalArgumentException: No view found for id xxxxx (android:id/content) for fragment 由二进制XML文件引起的IllegalArgumentException(Android) - IllegalArgumentException caused by binary XML file (Android) java.lang.IllegalArgumentException:n必须为正数Android Studio - java.lang.IllegalArgumentException: n must be positive Android Studio java.lang.IllegalArgumentException:android中的宽度和高度必须&gt; 0 - java.lang.IllegalArgumentException: width and height must be > 0 in android android studio picasso java.lang.IllegalArgumentException:目标不得为null - android studio picasso java.lang.IllegalArgumentException: Target must not be null Android java.lang.IllegalArgumentException:路径不能为空 - Android java.lang.IllegalArgumentException: Path must not be empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM