简体   繁体   English

带有ActionbarSherlock SearchView的ClassNotFoundException

[英]ClassNotFoundException with ActionbarSherlock SearchView

I currently have version 4.2.0 of ActionbarSherlock implemented in a Android application. 我目前在Android应用程序中实现了ActionbarSherlock的4.2.0版本。 And I'm having a issue with the SearchView widget. 我在使用SearchView小部件时遇到问题。

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: 现在,每当我开始调试应用程序时,在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. 我在应用程序支持的所有Android版本(2.2-4.2)中均发生此错误,当我在发生此异常后恢复调试时,应用程序运行良好,SearchView也正常运行,但是我仍然想知道为什么会发生此异常。

Please use this method by create menu in Xml: 请通过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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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