Logcat在SearchView类中出错

时间:2023-01-13 09:21:17

i have installed the support library to get the action bar worked also in android pre API 11

我已经安装了支持库以使操作条在android pre API 11中工作

When i start the application, the logcat give this error:

当我启动应用程序时,logcat会出现以下错误:

08-20 19:54:41.600: I/dalvikvm(9828): Failed resolving Landroid/support/v7/widget/SearchView$5; interface 809 'Landroid/view/View$OnLayoutChangeListener;'

08-20 19:54:41.600: W/dalvikvm(9828): Link of class 'Landroid/support/v7/widget/SearchView$5;' failed

08-20 19:54:41.600: E/dalvikvm(9828): Could not find class 'android.support.v7.widget.SearchView$5', referenced from method android.support.v7.widget.SearchView.addOnLayoutChangeListenerToDropDownAnchorSDK11

08-20 19:54:41.600: W/dalvikvm(9828): VFY: unable to resolve new-instance 734 (Landroid/support/v7/widget/SearchView$5;) in Landroid/support/v7/widget/SearchView;

08-20 19:54:41.600: D/dalvikvm(9828): VFY: replacing opcode 0x22 at 0x0002

08-20 19:54:41.600: D/dalvikvm(9828): VFY: dead code 0x0004-000a in Landroid/support/v7/widget/SearchView;.addOnLayoutChangeListenerToDropDownAnchorSDK11 ()V

Can someone help me, i search around the web but i found nothing. Thank you

有人能帮我吗?我在网上搜索,但什么也没找到。谢谢你!

MainActivity.Java

MainActivity.Java

    package com.example.fanculo;

    import android.os.Bundle;
    import android.app.SearchManager;
    import android.content.Context;
    import android.support.v4.view.MenuItemCompat;
    import android.support.v7.widget.SearchView;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.ActionBarActivity;
    import android.view.Menu;
    import android.view.MenuItem;


    public class MainActivity extends ActionBarActivity{

ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    actionBar = getSupportActionBar();
    actionBar.setTitle("Test");

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

     SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    // Configure the search info and add any event listeners
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
     searchView.setIconifiedByDefault(true); 
    return super.onCreateOptionsMenu(menu);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_search:
            onSearchRequested();
            return true;
        default:
            return false;
    }
}

}

}

3 个解决方案

#1


8  

I think this is a bug in SearchView.java class in support library, you can see that it is using View.OnLayoutChangeListener in a common implementation file:

我认为这是SearchView中的一个错误。在java类支持库中,您可以看到它正在使用视图。OnLayoutChangeListener在一个通用实现文件中:

https://android.googlesource.com/platform/frameworks/support.git/+/android-4.3_r1/v7/appcompat/src/android/support/v7/widget/SearchView.java

https://android.googlesource.com/platform/frameworks/support.git/ android + / - 4.3 - _r1 / v7 / appcompat / src / android /支持/ v7 /部件/ SearchView.java

this makes class loader to try loading View.OnLayoutChangeListener which is available since api level 11 - even if this *SDK11 method is not even called. I suppose this method addOnLayoutChangeListenerToDropDownAnchorSDK11 should be moved to external java class and used only if device API is >= 11.

这使得类装入器尝试装入视图。OnLayoutChangeListener,从api级别11开始可用——即使这个*SDK11方法甚至没有被调用。我认为应该将这个方法addonlayoutchangelistenertodownanchorsdk11转移到外部java类,并仅在设备API为>= 11时使用。

You can reproduce this bug copying this code to your own activity:

您可以复制这个错误,将此代码复制到您自己的活动中:

private void addOnLayoutChangeListenerToDropDownAnchorSDK11() {
    new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom,
                                   int oldLeft, int oldTop, int oldRight, int oldBottom) {
        }
    };
}

public void onCreate(...) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            addOnLayoutChangeListenerToDropDownAnchorSDK11();
        }
}

below is what logcat prints:

以下是logcat打印的内容:

08-31 22:50:33.030: INFO/dalvikvm(20753): Failed resolving Lcom/example/ActionBarTester/MyActivity$1; interface 813 'Landroid/view/View$OnLayoutChangeListener;'
08-31 22:50:33.030: WARN/dalvikvm(20753): Link of class 'Lcom/example/ActionBarTester/MyActivity$1;' failed
08-31 22:50:33.030: ERROR/dalvikvm(20753): Could not find class 'com.example.ActionBarTester.MyActivity$1', referenced from method com.example.ActionBarTester.MyActivity.addOnLayoutChangeListenerToDropDownAnchorSDK11
08-31 22:50:33.030: WARN/dalvikvm(20753): VFY: unable to resolve new-instance 903 (Lcom/example/ActionBarTester/MyActivity$1;) in Lcom/example/ActionBarTester/MyActivity;
08-31 22:50:33.030: DEBUG/dalvikvm(20753): VFY: replacing opcode 0x22 at 0x0000
08-31 22:50:33.030: DEBUG/dalvikvm(20753): VFY: dead code 0x0002-0005 in Lcom/example/ActionBarTester/MyActivity;.addOnLayoutChangeListenerToDropDownAnchorSDK11 ()V

I am not sure if this bug Is actually causing any problems, in my case SearchView works on API Level 10, also above tests allow my activity to work. Maybe I am missing something here.

我不确定这个bug是否真的造成了任何问题,在我的案例中,SearchView在API级别10上工作,同样在上面的测试允许我的活动工作。也许我漏掉了什么。

#2


0  

How about using ActionBarSherlock?? It's flexible and support older versions as well and also it's very easy to implement.

使用ActionBarSherlock ?怎么样?它很灵活,也支持旧版本,而且很容易实现。

All you have to do is to switch every class you're extending Activity in To SherlockActivity and Also fragment. I advice you to try it! https://github.com/JakeWharton/ActionBarSherlock

您所要做的就是将扩展活动的每个类切换到SherlockActivity和fragment。我建议你试试!https://github.com/JakeWharton/ActionBarSherlock

#3


-1  

You need to ensure you add the Android V7 Support Library correctly in Eclipse to remove the following error from the log 'Could not find class android.support.v7.widget.SearchView$5 referenced from method android.support.v7.widget.SearchView.addOnLayoutChangeListenerToDropDownAnchorSDK11'.

您需要确保在Eclipse中正确地添加了Android V7支持库,以从“无法找到Android . Support . V7 .widget”日志中删除以下错误。SearchView$5引用自android.support.v7.widget.SearchView.addOnLayoutChangeListenerToDropDownAnchorSDK11'方法。

The key thing to remember, don't forget to uncheck Android Dependencies when adding the Support Library because the v7 appcompat library has resources. After making the change to your dependencies in your support library project, clean the support library project and that's it.

要记住的关键是,在添加支持库时不要忘记取消对Android依赖项的检查,因为v7 appcompat库有资源。在对支持库项目中的依赖项进行更改之后,清理支持库项目,仅此而已。

Refer to complete procedure in section Adding Libraries with Resources of official Google doco on how to add support libraries with resources.

关于如何使用资源添加支持库,请参考谷歌官方文档中关于添加支持库的完整过程。

Excerpt from above referenced doco in case link changes in future:

摘自以上引用的doco在以后链路发生变化的情况下:

  1. Make sure you have downloaded the Android Support Library using the SDK Manager.
  2. 确保您已经使用SDK管理器下载了Android支持库。
  3. Create a library project and ensure the required JAR files are included in the project's build path:
    • Select File > Import.
    • 选择文件>导入。
    • Select Existing Android Code Into Workspace and click Next.
    • 在工作区中选择现有的Android代码并单击Next。
    • Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the appcompat project, browse to /extras/android/support/v7/appcompat/.
    • 浏览到SDK安装目录,然后浏览到Support Library文件夹。例如,如果您正在添加appcompat项目,请浏览到/附加/android/支持/v7/appcompat/。
    • Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.
    • 单击Finish以导入项目。对于v7 appcompat项目,您现在应该看到一个名为android-support-v7-appcompat的新项目。
    • In the new library project, expand the libs/ folder, right-click each .jar file and select Build Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar and android-support-v7-appcompat.jar files to the build path.
    • 在新的库项目中,展开libs/文件夹,右键单击每个.jar文件,并选择Build Path > Add to Build Path。例如,在创建v7 appcompat项目时,添加android-support-v4。jar和android-support-v7-appcompat。jar文件到构建路径。
    • Right-click the project and select Build Path > Configure Build Path. In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.
    • 右键单击项目并选择构建路径>配置构建路径。在Order和Export选项卡中,检查刚刚添加到构建路径的.jar文件,因此它们可以用于依赖这个库项目的项目。例如,appcompat项目要求您同时导出android-support-v4。jar和android-support-v7-appcompat。jar文件。
    • Uncheck Android Dependencies.
    • 取消Android依赖关系。
    • Click OK to complete the changes.
    • 单击OK完成更改。
  4. 创建一个库项目,并确保项目的构建路径中包含所需的JAR文件:选择File >导入。在工作区中选择现有的Android代码并单击Next。浏览到SDK安装目录,然后浏览到Support Library文件夹。例如,如果您正在添加appcompat项目,请浏览到/extras/android/support/v7/appcompat/。单击Finish以导入项目。对于v7 appcompat项目,您现在应该看到一个名为android-support-v7-appcompat的新项目。在新的库项目中,展开libs/文件夹,右键单击每个.jar文件,并选择Build Path > Add to Build Path。例如,在创建v7 appcompat项目时,添加android-support-v4。jar和android-support-v7-appcompat。jar文件到构建路径。右键单击项目并选择构建路径>配置构建路径。在Order和Export选项卡中,检查刚刚添加到构建路径的.jar文件,因此它们可以用于依赖这个库项目的项目。例如,appcompat项目要求您同时导出android-support-v4。jar和android-support-v7-appcompat。jar文件。取消Android依赖关系。单击OK完成更改。

#1


8  

I think this is a bug in SearchView.java class in support library, you can see that it is using View.OnLayoutChangeListener in a common implementation file:

我认为这是SearchView中的一个错误。在java类支持库中,您可以看到它正在使用视图。OnLayoutChangeListener在一个通用实现文件中:

https://android.googlesource.com/platform/frameworks/support.git/+/android-4.3_r1/v7/appcompat/src/android/support/v7/widget/SearchView.java

https://android.googlesource.com/platform/frameworks/support.git/ android + / - 4.3 - _r1 / v7 / appcompat / src / android /支持/ v7 /部件/ SearchView.java

this makes class loader to try loading View.OnLayoutChangeListener which is available since api level 11 - even if this *SDK11 method is not even called. I suppose this method addOnLayoutChangeListenerToDropDownAnchorSDK11 should be moved to external java class and used only if device API is >= 11.

这使得类装入器尝试装入视图。OnLayoutChangeListener,从api级别11开始可用——即使这个*SDK11方法甚至没有被调用。我认为应该将这个方法addonlayoutchangelistenertodownanchorsdk11转移到外部java类,并仅在设备API为>= 11时使用。

You can reproduce this bug copying this code to your own activity:

您可以复制这个错误,将此代码复制到您自己的活动中:

private void addOnLayoutChangeListenerToDropDownAnchorSDK11() {
    new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom,
                                   int oldLeft, int oldTop, int oldRight, int oldBottom) {
        }
    };
}

public void onCreate(...) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            addOnLayoutChangeListenerToDropDownAnchorSDK11();
        }
}

below is what logcat prints:

以下是logcat打印的内容:

08-31 22:50:33.030: INFO/dalvikvm(20753): Failed resolving Lcom/example/ActionBarTester/MyActivity$1; interface 813 'Landroid/view/View$OnLayoutChangeListener;'
08-31 22:50:33.030: WARN/dalvikvm(20753): Link of class 'Lcom/example/ActionBarTester/MyActivity$1;' failed
08-31 22:50:33.030: ERROR/dalvikvm(20753): Could not find class 'com.example.ActionBarTester.MyActivity$1', referenced from method com.example.ActionBarTester.MyActivity.addOnLayoutChangeListenerToDropDownAnchorSDK11
08-31 22:50:33.030: WARN/dalvikvm(20753): VFY: unable to resolve new-instance 903 (Lcom/example/ActionBarTester/MyActivity$1;) in Lcom/example/ActionBarTester/MyActivity;
08-31 22:50:33.030: DEBUG/dalvikvm(20753): VFY: replacing opcode 0x22 at 0x0000
08-31 22:50:33.030: DEBUG/dalvikvm(20753): VFY: dead code 0x0002-0005 in Lcom/example/ActionBarTester/MyActivity;.addOnLayoutChangeListenerToDropDownAnchorSDK11 ()V

I am not sure if this bug Is actually causing any problems, in my case SearchView works on API Level 10, also above tests allow my activity to work. Maybe I am missing something here.

我不确定这个bug是否真的造成了任何问题,在我的案例中,SearchView在API级别10上工作,同样在上面的测试允许我的活动工作。也许我漏掉了什么。

#2


0  

How about using ActionBarSherlock?? It's flexible and support older versions as well and also it's very easy to implement.

使用ActionBarSherlock ?怎么样?它很灵活,也支持旧版本,而且很容易实现。

All you have to do is to switch every class you're extending Activity in To SherlockActivity and Also fragment. I advice you to try it! https://github.com/JakeWharton/ActionBarSherlock

您所要做的就是将扩展活动的每个类切换到SherlockActivity和fragment。我建议你试试!https://github.com/JakeWharton/ActionBarSherlock

#3


-1  

You need to ensure you add the Android V7 Support Library correctly in Eclipse to remove the following error from the log 'Could not find class android.support.v7.widget.SearchView$5 referenced from method android.support.v7.widget.SearchView.addOnLayoutChangeListenerToDropDownAnchorSDK11'.

您需要确保在Eclipse中正确地添加了Android V7支持库,以从“无法找到Android . Support . V7 .widget”日志中删除以下错误。SearchView$5引用自android.support.v7.widget.SearchView.addOnLayoutChangeListenerToDropDownAnchorSDK11'方法。

The key thing to remember, don't forget to uncheck Android Dependencies when adding the Support Library because the v7 appcompat library has resources. After making the change to your dependencies in your support library project, clean the support library project and that's it.

要记住的关键是,在添加支持库时不要忘记取消对Android依赖项的检查,因为v7 appcompat库有资源。在对支持库项目中的依赖项进行更改之后,清理支持库项目,仅此而已。

Refer to complete procedure in section Adding Libraries with Resources of official Google doco on how to add support libraries with resources.

关于如何使用资源添加支持库,请参考谷歌官方文档中关于添加支持库的完整过程。

Excerpt from above referenced doco in case link changes in future:

摘自以上引用的doco在以后链路发生变化的情况下:

  1. Make sure you have downloaded the Android Support Library using the SDK Manager.
  2. 确保您已经使用SDK管理器下载了Android支持库。
  3. Create a library project and ensure the required JAR files are included in the project's build path:
    • Select File > Import.
    • 选择文件>导入。
    • Select Existing Android Code Into Workspace and click Next.
    • 在工作区中选择现有的Android代码并单击Next。
    • Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the appcompat project, browse to /extras/android/support/v7/appcompat/.
    • 浏览到SDK安装目录,然后浏览到Support Library文件夹。例如,如果您正在添加appcompat项目,请浏览到/附加/android/支持/v7/appcompat/。
    • Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.
    • 单击Finish以导入项目。对于v7 appcompat项目,您现在应该看到一个名为android-support-v7-appcompat的新项目。
    • In the new library project, expand the libs/ folder, right-click each .jar file and select Build Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar and android-support-v7-appcompat.jar files to the build path.
    • 在新的库项目中,展开libs/文件夹,右键单击每个.jar文件,并选择Build Path > Add to Build Path。例如,在创建v7 appcompat项目时,添加android-support-v4。jar和android-support-v7-appcompat。jar文件到构建路径。
    • Right-click the project and select Build Path > Configure Build Path. In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.
    • 右键单击项目并选择构建路径>配置构建路径。在Order和Export选项卡中,检查刚刚添加到构建路径的.jar文件,因此它们可以用于依赖这个库项目的项目。例如,appcompat项目要求您同时导出android-support-v4。jar和android-support-v7-appcompat。jar文件。
    • Uncheck Android Dependencies.
    • 取消Android依赖关系。
    • Click OK to complete the changes.
    • 单击OK完成更改。
  4. 创建一个库项目,并确保项目的构建路径中包含所需的JAR文件:选择File >导入。在工作区中选择现有的Android代码并单击Next。浏览到SDK安装目录,然后浏览到Support Library文件夹。例如,如果您正在添加appcompat项目,请浏览到/extras/android/support/v7/appcompat/。单击Finish以导入项目。对于v7 appcompat项目,您现在应该看到一个名为android-support-v7-appcompat的新项目。在新的库项目中,展开libs/文件夹,右键单击每个.jar文件,并选择Build Path > Add to Build Path。例如,在创建v7 appcompat项目时,添加android-support-v4。jar和android-support-v7-appcompat。jar文件到构建路径。右键单击项目并选择构建路径>配置构建路径。在Order和Export选项卡中,检查刚刚添加到构建路径的.jar文件,因此它们可以用于依赖这个库项目的项目。例如,appcompat项目要求您同时导出android-support-v4。jar和android-support-v7-appcompat。jar文件。取消Android依赖关系。单击OK完成更改。