0

我目前在 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 也工作正常,但我仍然想知道为什么会发生此异常。

4

1 回答 1

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);
    }
}
于 2013-01-01T12:37:49.580 回答