请帮忙。我得到这个错误,但不知道是怎么回事。我试图让我的片段搜索视图。
我使菜单项验证码
<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" />
使这个代码在我的片段
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)
}
但我得到这个错误
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)
请大家帮帮我是怎么回事
searchView.backgroundColorResource = Color.WHITE
的问题。你需要在这里设置一个资源ID,但Color.WHITE
是色彩值,而不是资源ID。你可以试试
searchView.backgroundColorResource = android.R.color.white
您使用的是Color
值,而不是资源引用,所以你不能使用
searchView.backgroundColorResource = Color.WHITE
作为替代亨利的回答,您可以使用旧的Java风格与方法
searchView.setBackgroundColor(Color.WHITE)
采用
getApplicationContext().getColor(R.color.white);
并把你的颜色值在XML文件中,如
RES /值/ colors.xml
这样看:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
//other colors...
</resources>