gpt4 book ai didi

java.lang.RuntimeException : Unable to start activity android. view.InflateException:二进制 XML 文件行 #13

转载 作者:行者123 更新时间:2023-12-01 19:34:14 25 4
gpt4 key购买 nike

我是 Android 新手。我会简短的。我收到底部提到的错误。我通过 XML 添加了一个 fragment 。我已经寻找了解决方案,但到目前为止没有一个有效。我哪里出错了?

如果我能找到一个解决方案,那就没问题了。我可以得到关于为什么会发生这种情况的解释吗?

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.workout, PID: 7653
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.workout/com.example.workout.DetailActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2969)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3047)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class fragment
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 com.example.workout.WorkoutDetailFragment
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3177)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:134)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:357)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:336)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:791)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.workout.DetailActivity.onCreate(DetailActivity.java:12)
at android.app.Activity.performCreate(Activity.java:7110)
at android.app.Activity.performCreate(Activity.java:7101)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2922)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3047)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1777)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:6861)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:450)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

详细 Activity Xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".DetailActivity">

<fragment
class="com.example.workout.WorkoutDetailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

DetailActivity Java

package com.example.workout;
import androidx.appcompat.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);
}
}

xml fragment

<!-- TODO: Update blank fragment layout -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textTitle"
android:text="@string/workout_title"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textDescription"
android:text="@string/workout_description"/>

</LinearLayout>

MainActivity Java

public class MainActivity extends AppCompatActivity {

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

public void onShowDetails(View view){
Intent intent = new Intent(this, DetailActivity.class);
startActivity(intent);
}
}

最佳答案

修复您的detail_activity.xml,将类替换为android:name

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".DetailActivity">

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

</androidx.constraintlayout.widget.ConstraintLayout>

关于java.lang.RuntimeException : Unable to start activity android. view.InflateException:二进制 XML 文件行 #13,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58429719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com