我正在尝试实现一个带有简单展开/收缩动画效果的搜索视图,但是在这个论坛中的所有答案都无法正常工作。
<item
android:id="@+id/action_search"
android:orderInCategory="100"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
android:icon="@drawable/ic_search_white_48px"
app:showAsAction="always"/>
我所做的
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
//Get the ID for the search bar LinearLayout
int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null);
//Get the search bar Linearlayout
LinearLayout searchBar = (LinearLayout)
searchView.findViewById(searchBarId);
//Give the Linearlayout a transition animation.
searchBar.setLayoutTransition(new LayoutTransition());
return true;
}
所有错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.daveslab.wideview, PID: 2459
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setLayoutTransition(android.animation.LayoutTransition)' on a null object reference
at com.daveslab.wideview.MainActivity.onCreateOptionsMenu(MainActivity.java:69)
at android.app.Activity.onCreatePanelMenu(Activity.java:2846)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:360)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:88)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:331)
at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:88)
at android.support.v7.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:454)
at android.support.v7.app.ToolbarActionBar$1.run(ToolbarActionBar.java:61)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
values.xml 中的错误(所有图标都已经以正确的名称在项目中):
Error:(1605, 21) No resource found that matches the given name: attr 'searchBackIcon'.
错误:(1602, 21)没有找到与给定名称匹配的资源:attr' searchBackground'。 错误:(1604, 21)没有找到与给定名称匹配的资源:attr' searchCloseIcon'。 错误:(1606, 21)没有找到与给定名称匹配的资源:attr' searchSuggestionBackground'。 错误:(1603, 21)没有找到与给定名称匹配的资源:attr' searchVoiceIcon'。 错误:(1605, 21)没有找到与给定名称匹配的资源:attr' searchBackIcon'。 错误:(1602, 21)没有找到与给定名称匹配的资源:attr' searchBackground'。 错误:(1604, 21)没有找到与给定名称匹配的资源:attr' searchCloseIcon'。 错误:(1606, 21)没有找到与给定名称匹配的资源:attr' searchSuggestionBackground'。 错误:(1603, 21)没有找到与给定名称匹配的资源:attr' searchVoiceIcon'。 错误:执行任务“:app:processDebugResources”失败。 com.android.ide.common.process.ProcessException:未能执行aapt。