我已经将使用Eclipse开发的Android应用程序转换为Android Studio。它的最低API级别为10(当前为22),并使用ActionBarSherlock(巨大的无赖)。使用Eclipse,它在库项目中使用了ActionBarSherlock 4.2,但现在似乎使用ActionBarSherlock的唯一方法是使用4.4作为带有gradle的AAR。 compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
在迁移完所有内容后,只要在Android 2.3.5上使用SearchView,应用程序就会崩溃(在Android 4上运行正常)。问题出在onCreateOptionsMenu menu.findItem(R.id.search_menu_item).getActionView(),它返回null。这是我的SearchActivity类的样子:
public class SearchActivity extends SherlockActivity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_view);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
mListView = (ListView) findViewById(R.id.search_view_list);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
getSupportMenuInflater().inflate(R.menu.search_menu, menu);
//Get the action view of the menu item whose id is search
SearchView searchView = (SearchView) menu.findItem(R.id.search_menu_item).getActionView();
searchView.setQueryHint("Search for ISBN, UPC, EAN or Title");
final Menu savedMenu = menu;
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextSubmit(String query) {
//Toast.makeText(getApplicationContext(), query, Toast.LENGTH_SHORT).show();
if (mOnlineSearchTask == null)
{
mOnlineSearchTask = new OnlineSearchTask(getBaseContext());
mOnlineSearchTask.execute(query, "title", "All");
((SearchView) savedMenu.findItem(R.id.search_menu_item).getActionView()).clearFocus();
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
}
);
searchView.setIconified(false);
return true;
}
}
search_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/search_view_list"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</ListView>
</RelativeLayout>
search_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<item android:id="@+id/search_menu_item"
android:title="Search"
android:icon="@drawable/ic_search_inverse"
android:showAsAction="always"
android:actionViewClass="com.actionbarsherlock.widget.SearchView" />
</menu>
答案 0 :(得分:0)
尝试将xml文件(search_menu.xml)更改为:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<item android:id="@+id/search_menu_item"
android:title="Search"
android:icon="@drawable/ic_search_inverse"
android:showAsAction="always"
app:actionViewClass="com.actionbarsherlock.widget.SearchView" />
</menu>
看起来几乎一样(因为它真的很相似) - 我将最后一行从android:actionViewClass....
更改为app:actionViewClass...