您好我想设置听众在Android 2.3中使用appcompat搜索编辑文本。
我的contact_menu.xml是
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_search"
android:orderInCategory="100"
android:title="@string/action_search"
myapp:actionViewClass="android.support.v7.widget.SearchView"
myapp:showAsAction="always"
android:actionLayout="@layout/actionbar_search"
/>
我的actionbar_search.xml是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="vertical" >
<EditText
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
和我的代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.contact_menu, menu);
View v = (View) menu.findItem(R.id.action_search).getActionView();
//** Get the edit text from the action view *
EditText txtSearch = ( EditText ) v.findViewById(R.id.search);
//** Setting an action listener *
txtSearch.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Toast.makeText(getBaseContext(), "Search : " + v.getText(), Toast.LENGTH_SHORT).show();
return false;
}
});
return true;
}
问题是当我在android 2.3模拟器中运行它时,我会给出此错误消息
11-17 07:31:05.893: E/AndroidRuntime(625): FATAL EXCEPTION: main
11-17 07:31:05.893: E/AndroidRuntime(625): java.lang.NoSuchMethodError: android.view.MenuItem.getActionView
11-17 07:31:05.893: E/AndroidRuntime(625): at com.rad.sarinab.contacts.onCreateOptionsMenu(contacts.java:172)
11-17 07:31:05.893: E/AndroidRuntime(625): at android.app.Activity.onCreatePanelMenu(Activity.java:2158)
11-17 07:31:05.893: E/AndroidRuntime(625): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
11-17 07:31:05.893: E/AndroidRuntime(625): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
11-17 07:31:05.893: E/AndroidRuntime(625): at android.support.v7.app.ActionBarActivityDelegateBase$1.run(ActionBarActivityDelegateBase.java:70)
11-17 07:31:05.893: E/AndroidRuntime(625): at android.os.Handler.handleCallback(Handler.java:587)
11-17 07:31:05.893: E/AndroidRuntime(625): at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 07:31:05.893: E/AndroidRuntime(625): at android.os.Looper.loop(Looper.java:123)
11-17 07:31:05.893: E/AndroidRuntime(625): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-17 07:31:05.893: E/AndroidRuntime(625): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 07:31:05.893: E/AndroidRuntime(625): at java.lang.reflect.Method.invoke(Method.java:507)
11-17 07:31:05.893: E/AndroidRuntime(625): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-17 07:31:05.893: E/AndroidRuntime(625): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-17 07:31:05.893: E/AndroidRuntime(625): at dalvik.system.NativeStart.main(Native Method)
更新
经过@blubrain的一些研究和有用的评论,当然this answer 我达到了这个代码:
我的菜单
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:sarinaB="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/add_item"
android:icon="@drawable/add"
android:orderInCategory="100"
sarinaB:showAsAction="always"/>
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
sarinaB:actionViewClass="android.support.v7.widget.SearchView"
android:orderInCategory="100"
android:title="@string/action_search"
sarinaB:showAsAction="ifRoom"/>
</menu>
我的oncreateoptionmenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.contact_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
Toast.makeText(getApplicationContext(), "dddddd "+text, Toast.LENGTH_LONG).show();
return false;
}
@Override
public boolean onQueryTextChange(String text) {
Toast.makeText(getApplicationContext(), "sssssss "+text, Toast.LENGTH_LONG).show();
return false;
}
});
return true;
}
答案 0 :(得分:0)
从Android documentation page引用:
If supporting API levels lower than 11:
- import android.support.v7.app.ActionBar
If supporting only API level 11 and higher:
- import android.app.ActionBar
确保您要导入正确的操作栏,请阅读the document。
其次要确保你正在调用正确的方法。对于2.3,您必须致电
MenuItemCompat.getActionView()
有关进一步说明:请查看this part of the document。
所以行
View v = (View) menu.findItem(R.id.action_search).getActionView();
应该是
View v = (View) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));