Android 错误:二进制XML文件行:正在膨胀类片段

Android 错误:二进制XML文件行:正在膨胀类片段,android,android-fragments,kotlin,layout-inflater,android-inflate,Android,Android Fragments,Kotlin,Layout Inflater,Android Inflate,我有一个activitymain activity,其中我有一个片段signup\u fragment我想从一个activity调用fragment,但它给出了一个异常,即Binary-XML文件行#23:Binary-XML文件行#23:Error-inflating class fragment 和由以下原因引起:java.lang.RuntimeException:com.example.PickComponents。MainActivity@d9ff1e7必须实现OnFragmentIn

我有一个activity
main activity
,其中我有一个片段
signup\u fragment
我想从一个activity调用fragment,但它给出了一个异常,即
Binary-XML文件行#23:Binary-XML文件行#23:Error-inflating class fragment

由以下原因引起:java.lang.RuntimeException:com.example.PickComponents。MainActivity@d9ff1e7必须实现OnFragmentInteractionListener

下面是我的代码

main活动

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()

    }
SignupFragment

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
    } }
活动\u 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>
<?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>

fragment\u signup.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>
<?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>


问题就在这里
com.example.pickComponents.ImagesliderFragment
。未找到此片段或该片段存在问题

您应该在xml中使用
FrameLayout
而不是
Fragment


此外,您的
main活动
应该在FragmentInteractionListener上实现

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

并使用
FrameLayout
而不是fragment

或者你可以使用下面的代码

<FrameLayout
        android:layout_width="362dp"
        android:layout_height="98dp"
        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"/>


您的XML在哪里?编辑后,这一行的问题仍然存在
android:name=“com.example.pickComponents.Signup”
它也在Signup.kt上给出了一个异常,
必须实现OnFragmentInteractionListener
我看到了您的代码。。据我所知,OnFragmentInteractionListener是不需要的,所以您可以删除与该侦听器相关的所有内容。