我有一个活动MainActivity
,其中有一个片段signup_fragment
,我想从活动中调用片段,但它给出了一个异常Binary XML file line #23: Binary XML file line #23: Error inflating class fragment
和Caused by: java.lang.RuntimeException: com.example.pickingredients.MainActivity@d9ff1e7 must implement OnFragmentInteractionListener
下面是我的代码
主要活动
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()
}
注册片段
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
} }
活动_主要.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
<?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>
2条答案
按热度按时间9rygscc11#
只需从〈fragment.../〉中删除
android:name
标记即可。并使用
FrameLayout
代替fragment。或者您可以使用下面的代码。
fdx2calv2#
问题出在**
com.example.pickingredients.ImagesliderFragment
。找不到此片段或该片段有问题。而且在xml中应该使用
FrameLayout
而不是Fragment
。此外,您的
MainActivity
应实现OnFragmentInteractionListener
**