[英]android.content.res.Resources$NotFoundException: Resource ID #0xffffffff. cannot make search view with query
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 请帮助我发生了什么
searchView.backgroundColorResource = Color.WHITE
is the problem. searchView.backgroundColorResource = Color.WHITE
是问题所在。 You need to set a resource id here but Color.WHITE
is a color value, not a resource id. 您需要在此处设置资源ID,但是Color.WHITE
是颜色值,而不是资源ID。 You can try 你可以试试
searchView.backgroundColorResource = android.R.color.white
You're using a Color
value, not a resource reference, so you can't use 您使用的是Color
值,而不是资源引用,因此您不能使用
searchView.backgroundColorResource = Color.WHITE
As an alternative to Henry's answer, you can use the old Java-style method with 作为亨利答案的替代方法,您可以将旧的Java样式方法与
searchView.setBackgroundColor(Color.WHITE)
Use 采用
getApplicationContext().getColor(R.color.white);
And put your color values in xml file, such as 并将您的颜色值放在xml文件中,例如
res/values/colors.xml RES /值/ colors.xml
looking like this: 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
//other colors...
</resources>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.