简体   繁体   中英

ClassNotFoundException with ActionbarSherlock SearchView

I currently have version 4.2.0 of ActionbarSherlock implemented in a Android application. And I'm having a issue with the SearchView widget.

I currently have the widget implemented like this:

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;
    }
}

Now whenever I start debugging the application I get the following error at the constructor of the 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]  

This error occurs within all the Android versions I support with the application (2.2 - 4.2) When I resume the debugging after this exception occurs the application runs fine and the SearchView works fine too, but I'm still wondering why this exception occurs.

Please use this method by create menu in 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>

add this code in your activity

@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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM