You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用FragmentContainerView时出现android.view.InflateException的求助

问题原因分析与解决方案

核心问题总结

  1. FragmentContainerView误用: FragmentContainerView是专门用于承载Fragment的容器,不允许直接在内部添加View组件(如你的FrameLayout)。所有UI元素应该属于Fragment的布局,而非Activity布局的直接子元素。
  2. MainActivity构造函数错误: 你在MainActivity的构造函数中传递布局参数给父类,这不符合Android组件的初始化规范。Activity的布局应在onCreate方法中设置。
  3. Fragment布局引用错误: ProductDetailFragment加载了Activity的布局文件,导致布局嵌套混乱,正确做法是为Fragment创建独立的布局文件。

修正后的代码

1. 创建Fragment专属布局文件 fragment_product_detail.xml

将原Activity布局中的UI元素全部移到这个文件中:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="30dp">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="@string/brand"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/item"
        android:textAppearance="@style/TextAppearance.AppCompat.Headline"
        android:textStyle="bold"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        app:srcCompat="@drawable/gibson_1"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/price"
        android:gravity="right"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:gravity="right">
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:layout_marginRight="15dp"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add to cart"
            android:drawableLeft="@drawable/drawablestart"
            android:id="@+id/addCart"/>
    </LinearLayout>

</FrameLayout>

2. 修正Activity布局 activity_main.xml

只保留FragmentContainerView作为容器:

<?xml version="1.0" encoding="utf-8"?>
<androidx.fragment.app.FragmentContainerView
    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=".MainActivity"
    android:id="@+id/main"/>

3. 修正MainActivity.java

移除错误的构造函数,在onCreate中正确设置布局:

package at.hlpinkafeld.android.musicstore;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .setReorderingAllowed(true)
                    .add(R.id.main, ProductDetailFragment.class, null)
                    .commit();
        }
    }
}

4. 修正ProductDetailFragment.java

改为加载Fragment自己的布局文件:

package at.hlpinkafeld.android.musicstore;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class ProductDetailFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_product_detail, container, false);
    }
}

内容的提问来源于stack exchange,提问作者ShinyStar MaybeNot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 04:30:30