Skip to content

FIXED What is wrong with my return in onCreateView

I have a problem with my Kotlin Android App. So far I haven’t written such a large project in Kotlin, so I totally don’t know what’s wrong.

When I try to start my application, I recive a wall of red lines. I found some solutions (for Java) and tried to implement them to my code, but it didn’t work.

    Process: com.example.noteapplication, PID: 11623
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.noteapplication/com.example.noteapplication.MainActivity}: android.view.InflateException: Binary XML file line #29: Binary XML file line #29: Error inflating class android.widget.SearchView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: android.view.InflateException: Binary XML file line #29: Binary XML file line #29: Error inflating class android.widget.SearchView
     Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class android.widget.SearchView
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
        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 com.example.noteapplication.HomeFragment.onCreateView(HomeFragment.kt:33)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:501)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1333)
        at android.app.Activity.performStart(Activity.java:6992)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2780)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
E/AndroidRuntime: Caused by: android.content.res.Resources$NotFoundException: Drawable com.example.noteapplication:string/search with resource ID #0x7f0f0079
    Caused by: android.content.res.Resources$NotFoundException: File Search your notes from drawable resource ID #0x7f0f0079
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:768)
        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:600)
        at android.content.res.Resources.loadDrawable(Resources.java:885)
        at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:953)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:928)
        at android.widget.SearchView.<init>(SearchView.java:296)
        at android.widget.SearchView.<init>(SearchView.java:258)
        at android.widget.SearchView.<init>(SearchView.java:254)
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
        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 com.example.noteapplication.HomeFragment.onCreateView(HomeFragment.kt:33)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:501)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1333)
        at android.app.Activity.performStart(Activity.java:6992)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2780)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
     Caused by: java.io.FileNotFoundException: Search your notes
        at android.content.res.AssetManager.openNonAssetNative(Native Method)
        at android.content.res.AssetManager.openNonAsset(AssetManager.java:466)
        at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:761)
        at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:600) 
        at android.content.res.Resources.loadDrawable(Resources.java:885) 
        at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:953) 
        at android.content.res.TypedArray.getDrawable(TypedArray.java:928) 
        at android.widget.SearchView.<init>(SearchView.java:296) 
        at android.widget.SearchView.<init>(SearchView.java:258) 
        at android.widget.SearchView.<init>(SearchView.java:254) 
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:647) 
        at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:720) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788) 
        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 com.example.noteapplication.HomeFragment.onCreateView(HomeFragment.kt:33) 
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963) 
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518) 
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282) 
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189) 
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100) 
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002) 
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3138) 
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3072) 
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:251) 
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:501) 
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:246) 
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1333) 
        at android.app.Activity.performStart(Activity.java:6992) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2780) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6541) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

and I think that the problem is in HomeFragment.kt:33 (in a fun onCreateView, exactly in return)

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SearchView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.codingwithme.notesapp.entities.Notes
import com.example.noteapplication.adapter.NotesAdapter
import com.example.noteapplication.database.NotesDatabase
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.coroutines.launch
import java.util.*
import kotlin.collections.ArrayList

class HomeFragment : BaseFragment() {
    var arrNotes = ArrayList<Notes>()
    var notesAdapter: NotesAdapter = NotesAdapter()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
        }
    }


    override fun onCreateView (inflater: LayoutInflater,
                               container: ViewGroup?,
                               savedInstanceState: Bundle?):
            View? {
        return inflater.inflate(R.layout.fragment_home, container, false)
    }

    companion object {
        @JvmStatic
        fun newInstance() =
                HomeFragment().apply {
                    arguments = Bundle().apply {

                    }
                }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        recycler_view.setHasFixedSize(true)

        recycler_view.layoutManager = StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL)

        launch {
            context?.let {
                var notes = NotesDatabase.getDatabase(it).noteDao().getAllNotes()
                //calling setData function
                notesAdapter!!.setData(notes)
                arrNotes = notes as ArrayList<Notes>
                recycler_view.adapter = notesAdapter
            }
        }
        notesAdapter!!.setOnClickListener(onClicked)

        fabBtnCreateNote.setOnClickListener{
            replaceFragment(CreateNoteFragment.newInstance(), true)
        }

        search_view.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {

                var tempArr = ArrayList<Notes>()

                for(arr in arrNotes){
                    if (arr.title!!.toLowerCase(Locale.getDefault()).contains(newText.toString())){
                        tempArr.add(arr)
                    }
                }

                notesAdapter.setData(tempArr)
                notesAdapter.notifyDataSetChanged()
                return true
            }

        })
    }

    private val onClicked = object :NotesAdapter.OnItemClickListener{
        override fun onClicked(notesId: Int) {
            var fragment :Fragment
            var bundle = Bundle()
            bundle.putInt("noteId",notesId)
            fragment = CreateNoteFragment.newInstance()
            fragment.arguments = bundle

            replaceFragment(fragment,false)
        }

    }


    fun replaceFragment(fragment: Fragment, istransition:Boolean){
        val fragmentTransition = requireActivity().supportFragmentManager.beginTransaction()

        if(istransition){
            fragmentTransition.setCustomAnimations(android.R.anim.slide_out_right,android.R.anim.slide_in_left)
        }
        fragmentTransition.replace(R.id.frame_layout,fragment).addToBackStack(fragment.javaClass.simpleName).commit()
    }
}

Can somebody tell me where is a problem and how can I fix it? (below a fragment_home.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:background="@color/ColorLightBlack"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:fontFamily="@font/montserrat_bold"
        android:paddingStart="@dimen/_11sdp"
        android:paddingTop="@dimen/_11sdp"
        android:paddingEnd="@dimen/_11sdp"
        android:text="@string/notes"
        android:textColor="@color/ColorWhite"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <SearchView
        android:id="@+id/search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:background="@drawable/background"
        android:searchHintIcon="@string/search"
        android:searchIcon="@drawable/ic_search"
        android:theme="@style/ThemeOverlay.search"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv1" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="378dp"
        android:layout_height="181dp"
        android:layout_margin="@dimen/_11sdp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/search_view" />

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="412dp"
        android:layout_height="38dp"
        android:background="@drawable/background"
        android:orientation="horizontal"
        android:padding="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="InvalidId">

        <ImageView
            android:layout_width="35dp"
            android:layout_height="match_parent"
            android:contentDescription="TODO"
            android:src="@drawable/ic_tick"
            app:tint="@color/ColorWhite" />

        <ImageView
            android:layout_width="35dp"
            android:layout_height="match_parent"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:src="@drawable/ic_image"
            app:tint="@color/ColorWhite"
            android:contentDescription="TODO" />

        <ImageView
            android:layout_width="35dp"
            android:layout_height="match_parent"
            android:src="@drawable/ic_link"
            app:tint="@color/ColorWhite"
            android:contentDescription="TODO" />
    </LinearLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabBtnCreateNote"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="8dp"
        android:backgroundTint="@color/ColorWhite"
        android:clickable="true"
        android:src="@drawable/ic_add"
        android:tint="@color/ColorYellow"
        app:backgroundTint="@color/ColorYellow"
        app:layout_constraintBottom_toTopOf="@id/linear1"
        app:layout_constraintEnd_toEndOf="parent"
        app:rippleColor="@color/ColorBlack"
        android:contentDescription="@string/todo" />


</androidx.constraintlayout.widget.ConstraintLayout>

[FIXED] I fix my mistake (pointed in first comment) and delete tools:context=”.MainActivity” from fragment xml

Answer

 android:searchHintIcon="@string/search"

This line is wrong. searchHintIcon is a drawable, not string. You can put something like

 android:searchHintIcon="@drawable/ic_search"