我正在关注ActionBarCompat的教程,该教程也启用了搜索文本区域。我从v7支持lib扩展了ActionBarActivity。当我尝试覆盖这两个方法时,它无法在超类中找到它们。我去了源代码并查看了超类的方法,我也找不到它们。这两种方法是onQueryTextSubmit
和onQueryTextChange
This was the tutorial.
知道我做错了吗?
当我将鼠标悬停在方法上时,这是错误:
The method onQueryTextChange(String) of type MainActivity must override or implement a supertype method
public class MainActivity extends ActionBarActivity{
private SearchView mSearchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
}
@Override
public boolean onQueryTextSubmit(String s) {
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
return true;
}
@Override
public boolean onQueryTextChange(String s) {
return false;
}
@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);
MenuItem searchItem=menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_search:
mSearchView.setIconified(false);
return true;
}
return false;
}
答案 0 :(得分:3)
听起来你没有实现这些方法所需的类。
public class MainActivity extends ActionBarActivity implements SearchView.OnQueryTextListener
确保您已实施SearchView.OnQueryTextListener
。