特定设备 androidx 中的搜索视图问题
searchview issue in a specific device androidx
我已将我的代码移至 androidx,它在其他设备上工作正常,但我得到
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference at packagename.FileName.onCreateOptionsMenu (FileName.java:43)
在 VIVO 设备
以下是我的菜单代码
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_search_category_default"
android:orderInCategory="100"
android:title="@string/w_search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
我无法弄清楚发生这种情况的原因。
编辑:
对于上下文,如果有帮助的话,我在 com.google.android.material.textfield.TextInputLayout
中遇到了类似的问题,但是在添加 app:passwordToggleDrawable="@null"
之后,问题得到了解决
编辑 2:
搜索查看代码
getMenuInflater().inflate(R.menu.menu_search, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setQueryHint(getResources().getString(R.string.w_hint_search));
SearchView.SearchAutoComplete searchAutoComplete = searchView.findViewById(R.id.search_src_text);
searchAutoComplete.setHintTextColor(Color.parseColor("#5FFFFFFF"));
searchAutoComplete.setTextColor(Color.WHITE);
根据评论中的一些建议,我无法找到解决问题的具体答案
首先,我将菜单从 androidx.appcompat.widget.SearchView
更改为 android.widget.SearchView
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_search_category_default"
android:orderInCategory="100"
android:title="@string/w_search"
app:showAsAction="always"
app:actionViewClass="android.widget.SearchView" />
并将我的 java
导入从 import androidx.appcompat.widget.SearchView;
更改为 import android.widget.SearchView;
从技术上讲,它没有回答问题,但解决了我的问题,所以我将其作为答案发布。
我已将我的代码移至 androidx,它在其他设备上工作正常,但我得到
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference at packagename.FileName.onCreateOptionsMenu (FileName.java:43)
在 VIVO 设备
以下是我的菜单代码
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_search_category_default"
android:orderInCategory="100"
android:title="@string/w_search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
我无法弄清楚发生这种情况的原因。
编辑:
对于上下文,如果有帮助的话,我在 com.google.android.material.textfield.TextInputLayout
中遇到了类似的问题,但是在添加 app:passwordToggleDrawable="@null"
之后,问题得到了解决
编辑 2:
搜索查看代码
getMenuInflater().inflate(R.menu.menu_search, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setQueryHint(getResources().getString(R.string.w_hint_search));
SearchView.SearchAutoComplete searchAutoComplete = searchView.findViewById(R.id.search_src_text);
searchAutoComplete.setHintTextColor(Color.parseColor("#5FFFFFFF"));
searchAutoComplete.setTextColor(Color.WHITE);
根据评论中的一些建议,我无法找到解决问题的具体答案
首先,我将菜单从 androidx.appcompat.widget.SearchView
更改为 android.widget.SearchView
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_search_category_default"
android:orderInCategory="100"
android:title="@string/w_search"
app:showAsAction="always"
app:actionViewClass="android.widget.SearchView" />
并将我的 java
导入从 import androidx.appcompat.widget.SearchView;
更改为 import android.widget.SearchView;
从技术上讲,它没有回答问题,但解决了我的问题,所以我将其作为答案发布。