Android Fragments java.lang.RuntimeException:无法启动活动android.view,InflateException:二进制XML文件行#13

nmpmafwu  于 2023-08-06  发布在  Android
关注(0)|答案(5)|浏览(133)

我是学Android的新手。我长话短说。我收到了底部提到的错误。我通过XML添加了一个片段。我一直在寻找解决方案,但到目前为止没有一个成功。我哪里做错了?
如果我能找到解决办法那就好。我能解释一下为什么会这样吗?

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)

字符串

DetailActivity 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

<!-- 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);
 }
}

sf6xfgos

sf6xfgos1#

修复你的detail_activity.xml将class替换为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>

字符串

8cdiaqws

8cdiaqws2#

修改DetailActivity.xml标记片段,如下所示

<fragment android:name="com.example.workout.WorkoutDetailFragment"
    android:id="@+id/frag"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

字符串

mpgws1up

mpgws1up3#

使用OnFragmentInteractionListener实现您的主要活动,并按照以下代码更改xml

public class MainActivity extends AppCompatActivity implements WorkoutDetailFragment.OnFragmentInteractionListener {

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

@Override
public void onFragmentInteraction(Uri uri) {

}}

字符串
你的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=".MainActivity">

<fragment
    android:id="@+id/fragment"
    android:name="com.example.myapplication.WorkoutDetailFragment"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


如果仍然无法执行,请共享片段类文件

knpiaxh1

knpiaxh14#

  • 此错误主要是因为您没有为片段容器给予id。*
    在DetailActivity.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.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
     android:id="@+id/uniqueid" //this was missing
     class="com.example.workout.WorkoutDetailFragment"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
/>

moiiocjp

moiiocjp5#

请为您的片段指定唯一的id并尝试。

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

字符串

相关问题