“未解析的类‘PlaceHolderFragment’”

CJR*_*CJR 1 android android-layout android-fragments

我的布局文件中出现错误:

<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.myapp.MainActivity$PlaceholderFragment">

    <!-- Some stuff here -->

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

它在这一行给了我一个错误:

tools:context="com.myapp.MainActivity$PlaceholderFragment"
Run Code Online (Sandbox Code Playgroud)

错误如下:

未解析的类“PlaceHolderFragment”
Android XML 文件内的有效资源引用。

我可以构建我的应用程序并运行它,但我在 Stackoverflow 和其他地方没有找到任何有助于解决我的错误的内容。有人知道我为什么会收到此错误吗?

Mar*_*ski 5

您可以安全地tools:context从该 XML 中删除条目,因为这只是让您的 Android Studio 了解该布局的上下文中的内容(这里是受支持标签的官方文档tools:...),但是您绝对应该避免将片段作为 Activity 的内部类(并且作为一般情况下的内部类),因为这会给您带来更多麻烦,因此我强烈建议重构您的代码并将您的代码PlaceholderFragment与活动类代码完全分离。