我通過基於this tutorial的內容提供者實現了自定義建議;它的工作原理,但我有一個問題樣式的建議列表。我目前的建議清單有黑色背景和白色文本。我想將其更改爲白色背景和黑色文本。我做了一些網上搜索,並試圖在styles.xml文件是這樣的:如何在Android中自定義搜索建議列表的樣式
<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
<item name="suggestionRowLayout">@layout/list_item_search_suggestion</item>
</style>
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="searchViewStyle">@style/MySearchViewStyle</item>
</style>
我list_item_search_suggestion看起來是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@android:id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="center_vertical"
android:minHeight="56dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@android:color/black"
tools:text="Blah blah blah"/>
然而,當我運行的應用程序和搜索的東西,它崩潰之前該建議列表出現。在SuggestionsAdapter中,我從newView方法中得到一個NullPointerException異常。這個SuggestionAdapter來自SDK,我沒有實現我自己的適配器。我只是實現內容提供者並將其設置在searchable.xml中。堆棧軌道是這樣的:
06-15 21:11:09.253 4063-4063/com.tophatter E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.tophatter, PID: 4063
java.lang.NullPointerException
at android.support.v7.widget.SuggestionsAdapter.newView(SuggestionsAdapter.java:249)
at android.support.v7.widget.SuggestionsAdapter.getView(SuggestionsAdapter.java:453)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1585)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1167)
at android.widget.ListPopupWindow.show(ListPopupWindow.java:554)
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1096)
at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:971)
at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:953)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
如果您發佈堆棧跟蹤會很有幫助。 – Skynet
請張貼您的適配器代碼和您正在充氣的視圖,以便我可以幫助您 – AnswerDroid
同樣在這裏,我想將「light」主題應用於建議行,爲searchview widget維護AppCompat.Dark.ActionBar 。你可以發佈你的list_item_search_suggestion佈局嗎? – Jumpa