0

please help. i got this error but don't know what is going on. i tried to make a search view in my fragment.

i make this code for menu item

<item
    android:id="@+id/action_search"
    android:icon="@drawable/ic_search"
    android:title="search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView" />

and make this code in my fragment

 override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
    menu?.clear()
    inflater?.inflate(R.menu.menu_home, menu)
    val searchItem = menu?.findItem(R.id.action_search)
    val searchView = MenuItemCompat.getActionView(searchItem) as SearchView
    searchView.backgroundColorResource = Color.WHITE
    searchView.queryHint = "Search Review Title"
    searchView.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String?): Boolean {
            presenter.getSearchReviewList(query)
            return true
        }

        override fun onQueryTextChange(query: String?): Boolean {
            presenter.getSearchReviewList(query)
            return true
        }
    })
    super.onCreateOptionsMenu(menu, inflater)
}

but i got this error

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.mqa.android.moviereview, PID: 22280
              android.content.res.Resources$NotFoundException: Resource ID #0xffffffff
                  at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:204)
                  at android.content.res.Resources.getColor(Resources.java:949)
                  at android.content.res.Resources.getColor(Resources.java:925)
                  at org.jetbrains.anko.CustomViewPropertiesKt.setBackgroundColorResource(CustomViewProperties.kt:36)
                  at com.mqa.android.moviereview.module.fragment.home.HomeFragment.onCreateOptionsMenu(HomeFragment.kt:97)
                  at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:2561)
                  at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:3321)
                  at android.support.v4.app.FragmentController.dispatchCreateOptionsMenu(FragmentController.java:331)
                  at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:379)
                  at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
                  at android.support.v7.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2549)
                  at android.support.v7.app.AppCompatDelegateImpl.preparePanel(AppCompatDelegateImpl.java:1589)
                  at android.support.v7.app.AppCompatDelegateImpl.doInvalidatePanelMenu(AppCompatDelegateImpl.java:1869)
                  at android.support.v7.app.AppCompatDelegateImpl$2.run(AppCompatDelegateImpl.java:227)
                  at android.os.Handler.handleCallback(Handler.java:789)
                  at android.os.Handler.dispatchMessage(Handler.java:98)
                  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)

please help me what is going on

Qube
  • 543
  • 3
  • 9
  • 23
  • Check if `SearchView` imported in activity is same as `v7.widget.SearchView`. – Ranjan Feb 04 '19 at 12:39
  • same problem solved -> https://stackoverflow.com/questions/15262261/android-content-res-resourcesnotfoundexception-unable-to-find-resource-id-0xf – busetekin Feb 04 '19 at 13:17

3 Answers3

3

searchView.backgroundColorResource = Color.WHITE is the problem. You need to set a resource id here but Color.WHITE is a color value, not a resource id. You can try

searchView.backgroundColorResource = android.R.color.white
Henry
  • 42,982
  • 7
  • 68
  • 84
1

You're using a Color value, not a resource reference, so you can't use

searchView.backgroundColorResource = Color.WHITE

As an alternative to Henry's answer, you can use the old Java-style method with

searchView.setBackgroundColor(Color.WHITE)
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
0

Use

getApplicationContext().getColor(R.color.white);

And put your color values in xml file, such as

res/values/colors.xml

looking like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#ffffff</color>
    //other colors...
</resources>
  • As a side note, `white` is already defined in [android.R.color](https://developer.android.com/reference/android/R.color) – Michael Dodd Feb 04 '19 at 12:46