我目前在Android应用程序中实现了版本4.2.0的ActionbarSherlock。 我遇到了SearchView小部件的问题。
我目前正在实现这样的小部件:
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.widget.SearchView;
import com.actionbarsherlock.widget.SearchView.OnQueryTextListener;
public class MainActivity extends SherlockFragmentActivity
{
/*...*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint(getResources().getString(R.string.searchHint_Label));
searchView.setIconifiedByDefault(true);
searchView.setOnQueryTextListener(new OnQueryTextListener(){
@Override
public boolean onQueryTextSubmit(String query) {
// Do stuff
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
return false;
}
});
menu.add("Search")
.setIcon(R.drawable.abs__ic_search)
.setActionView(searchView)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
return true;
}
}
现在每当我开始调试应用程序时,我都会在SearchView的构造函数中收到以下错误:
01-01 11:46:40.365: E/dalvikvm(1385): Could not find class 'com.actionbarsherlock.widget.SearchView$11', referenced from method com.actionbarsherlock.widget.SearchView.<init>
Call Stack:
Thread [<1> main] (Suspended (exception ClassNotFoundException))
<VM does not provide monitor information>
PathClassLoader.findClass(String) line: 240
PathClassLoader(ClassLoader).loadClass(String, boolean) line: 551
PathClassLoader(ClassLoader).loadClass(String) line: 511
DexFile.defineClass(String, ClassLoader, int, ProtectionDomain) line: not available [native method]
DexFile.loadClassBinaryName(String, ClassLoader) line: 207
PathClassLoader.findClass(String) line: 200
PathClassLoader(ClassLoader).loadClass(String, boolean) line: 551
PathClassLoader(ClassLoader).loadClass(String) line: 511
MainActivity.onCreateOptionsMenu(Menu) line: 124
MainActivity(Watson).onCreatePanelMenu(int, Menu) line: 45
ActionBarSherlockCompat(ActionBarSherlock).callbackCreateOptionsMenu(Menu) line: 559
ActionBarSherlockCompat.preparePanel() line: 479
ActionBarSherlockCompat.dispatchInvalidateOptionsMenu() line: 272
ActionBarSherlockCompat$1.run() line: 984
ViewRoot(Handler).handleCallback(Message) line: 587
ViewRoot(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 130
ActivityThread.main(String[]) line: 3683
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 839
ZygoteInit.main(String[]) line: 597
NativeStart.main(String[]) line: not available [native method]
我在应用程序支持的所有Android版本中都会出现此错误(2.2 - 4.2) 当我在发生此异常后恢复调试时,应用程序运行良好,SearchView也正常工作,但我仍然想知道为什么会发生此异常。
答案 0 :(得分:0)
请通过Xml中的创建菜单使用此方法:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/search"
android:icon="@drawable/abs__ic_search"
android:showAsAction="add here your action"
android:title="@string/search">
</item>
</menu>
在您的活动中添加此代码
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
add code here
return true;
default:
return super.onOptionsItemSelected(item);
}
}