android - 错误 :Binary XML file line : Inflating class fragment

标签 android android-fragments kotlin layout-inflater android-inflate

我有一个 Activity MainActivity,其中有 fragment signup_fragment 我想从一个 Activity 中调用 fragment ,但它给出了 Binary XML 文件行的异常#23:二进制 XML 文件第 23 行:扩展类 fragment 时出错

Caused by: java.lang.RuntimeException: com.example.pickingredients.MainActivity@d9ff1e7 must implement OnFragmentInteractionListener

下面是我的代码

主 Activity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val fragment=Signup()
            supportFragmentManager.beginTransaction().add(R.id.sigup_fragment,fragment).commit()

    }

注册 fragment

class Signup : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null
    private var listener: OnFragmentInteractionListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_signup, container, false)
    }

    // TODO: Rename method, update argument and hook method into UI event
    fun onButtonPressed(uri: Uri) {
        listener?.onFragmentInteraction(uri)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is OnFragmentInteractionListener) {
            listener = context
        } else {
            throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
        }
    }

    override fun onDetach() {
        super.onDetach()
        listener = null
    } }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:background="@color/light_grey">

    <TextView
            android:text="@string/what_is_your_mood"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:id="@+id/mood_tv"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
            app:layout_constraintHorizontal_bias="0.03"
            android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent"
            android:textSize="18sp"/>
    <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content"
                                                android:id="@+id/guideline" app:layout_constraintGuide_begin="16dp"
                                                android:orientation="vertical"/>
    <fragment
            android:layout_width="362dp"
            android:layout_height="98dp" android:name="com.example.pickingredients.ImagesliderFragment"
            android:id="@+id/sigup_fragment" app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp" app:layout_constraintStart_toStartOf="@+id/guideline"
            android:layout_marginStart="8dp" app:layout_constraintTop_toBottomOf="@+id/mood_tv"
            android:layout_marginTop="16dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

fragment_signup.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".Signup">

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"/>

</FrameLayout>

最佳答案

只需从 .. 中删除 android:name 标签

并使用 FrameLayout 而不是 fragment。

或者您可以使用下面的代码。

<FrameLayout
        android:layout_width="362dp"
        android:layout_height="98dp"
        android:id="@+id/sigup_fragment"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toBottomOf="@+id/mood_tv" />

关于android - 错误 :Binary XML file line : Inflating class fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58285078/

相关文章:

android - 使用触摸事件在 Android 中进行反向地理编码

android - 如何在抽屉导航布局中创建多级菜单

android - Kotlin - 通过 Intent 将函数作为参数传递

java - 从图库中选择的图像出现 Kotlin 错误

java - 安卓 :Parse XML Data: Tags with arguments

android - 从原生 c 代码 android 创建文件

使用 'ionic emulate android' 命令时 Android 模拟器不安装应用程序

android - FragmentManager提交后执行功能

android - android中 fragment/对话框之间的通信

android - Koin 构造函数注入(inject)少量参数 MVVM Android 架构