android - 为什么我会收到 Error inflating class fragment?

标签 android android-fragments

我有一个包含 fragment 的 Activity 。

Activity 看起来:

activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="keepInTouch.mainScreen.MainActivity">



    <TextView
        android:id="@+id/textV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <fragment
        android:id="@+id/fragmnet_main_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment_main_list" />
</LinearLayout>


public class MainActivity extends AppCompatActivity {

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

fragment 外观: fragment_main_list.xml

<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"
    android:orientation="vertical"
    tools:context="keepInTouch.today.MainListFragment">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/mainFragmentListTitle"
        android:background="#e43d3d"
        android:textColor="#371b1b" />


    <ListView
        android:id="@+id/lvKits"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


public class MainListFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main_list, container, false);
    }

我收到以下错误: android.view.InflateException:二进制 XML 文件第 41 行:错误膨胀类 fragment

我已经尝试更换 MainActivity 扩展 AppCompatActivity 和: MainActivity 扩展了 FragmentActivity

但我得到了相同的结果。

我做错了什么?

最佳答案

您忘记了 fragment 中的 android:name 属性。这样做:

<fragment 
        android:id="@+id/fragmnet_main_list" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:name="keepInTouch.today.MainListFragment"
        tools:layout="@layout/fragment_main_list" />

关于android - 为什么我会收到 Error inflating class fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39297597/

相关文章:

java - 如何在线程结束时执行函数?

android - 导航组件生命周期

android - fragment 是否可以扩展 fragment ?

android - 访问 iOS 指纹的哈希值?

java.lang.RuntimeException : Caused by: java. lang.IllegalStateException:

android - 为什么在我的布局中我无法将 ImageView 和 TextView 对齐?

java - 异步任务 fragment 后台数据

android - 状态栏下的 Material design 抽屉 : how to achieve it using a DrawerLayout with two FrameLayouts as children?

android - 在 fragment 中使用 fragment

android - 调用子 fragment 时找不到 id 的 View