У меня есть MainActivity
, в котором я вызываю fragmrnt ImagesliderFragment
, но он выдает исключение Fragment com.example.pickingredients.ImagesliderFragment did not create a view.
, вот некоторые части моего кода
MainACctivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(savedInstanceState==null) {
val fragment = ImagesliderFragment()
supportFragmentManager.beginTransaction().add(R.id.fragment, fragment).commit()
}
}
ImagesliderFragment
class ImagesliderFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
inflater.inflate(R.layout.fragment_imageslider, container, false)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val frag_rv = view.findViewById<RecyclerView>(R.id.frag_rv)
frag_rv.layoutManager = LinearLayoutManager(context, LinearLayout.HORIZONTAL,
false)
displayContent()
}
fun displayContent(){
val contentList:ArrayList<Int> = ArrayList()
contentList.add(R.drawable.italian)
contentList.add(R.drawable.chinese)
contentList.add(R.drawable.dessert)
contentList.add(R.drawable.sushi)
contentList.add(R.drawable.desi)
frag_rv.adapter = ViewAdapter(contentList)
}}
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/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>
Ошибка
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pickingredients, PID: 2506
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pickingredients/com.example.pickingredients.MainActivity}: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3114)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class fragment
Caused by: java.lang.IllegalStateException: Fragment com.example.pickingredients.ImagesliderFragment did not create a view.
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3778)
at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:120)
at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:405)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:387)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.pickingredients.MainActivity.onCreate(MainActivity.kt:17)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)