使用来自支持库的GridLayout会导致NoSuchMethodError:android.support.v4.view.ViewCompat.getLayoutDirection'

时间:2014-03-27 08:14:31

标签: android android-support-library android-gridlayout

我在我的应用程序中使用GridLayout,它完美无缺。现在我也必须支持android 3.xx,所以我加入了android-support-library-v7。我刚刚在所有地方更改了GridLayout和LayoutParams的import语句。我现在可以构建项目,但是当我运行应用程序时,它会因NoSuchMethodError异常而崩溃。

我遵循了这些steps to include the support library

这是stacktrace:

    03-27 13:37:13.454: E/AndroidRuntime(23016): FATAL EXCEPTION: main
03-27 13:37:13.454: E/AndroidRuntime(23016): java.lang.NoSuchMethodError: android.support.v4.view.ViewCompat.getLayoutDirection
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.support.v7.widget.GridLayout.isLayoutRtlCompat(GridLayout.java:675)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.support.v7.widget.GridLayout.getDefaultMargin(GridLayout.java:660)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.support.v7.widget.GridLayout.getMargin1(GridLayout.java:671)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.support.v7.widget.GridLayout$Axis.computeMargins(GridLayout.java:1547)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.support.v7.widget.GridLayout$Axis.getLeadingMargins(GridLayout.java:1558)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.support.v7.widget.GridLayout.getMargin(GridLayout.java:683)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.support.v7.widget.GridLayout.getTotalMargin(GridLayout.java:692)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.support.v7.widget.GridLayout.measureChildWithMargins2(GridLayout.java:897)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.support.v7.widget.GridLayout.measureChildrenWithMargins(GridLayout.java:911)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.support.v7.widget.GridLayout.onMeasure(GridLayout.java:950)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.measure(View.java:15848)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.measure(View.java:15848)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.HorizontalScrollView.measureChildWithMargins(HorizontalScrollView.java:1229)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.HorizontalScrollView.onMeasure(HorizontalScrollView.java:301)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.measure(View.java:15848)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.measure(View.java:15848)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.measure(View.java:15848)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.measure(View.java:15848)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.ListView.setupChild(ListView.java:1918)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.ListView.makeAndAddView(ListView.java:1843)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.ListView.fillDown(ListView.java:675)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.ListView.fillSpecific(ListView.java:1333)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.ListView.layoutChildren(ListView.java:1633)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.AbsListView.onLayout(AbsListView.java:2012)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.layout(View.java:14289)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.ViewGroup.layout(ViewGroup.java:4559)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.layout(View.java:14289)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.ViewGroup.layout(ViewGroup.java:4559)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.layout(View.java:14289)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.ViewGroup.layout(ViewGroup.java:4559)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.layout(View.java:14289)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.ViewGroup.layout(ViewGroup.java:4559)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.layout(View.java:14289)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.ViewGroup.layout(ViewGroup.java:4559)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.View.layout(View.java:14289)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.ViewGroup.layout(ViewGroup.java:4559)
03-27 13:37:13.454: E/AndroidRuntime(23016):    at android.view.ViewRootImpl.perfor

编辑:代码

public class TableRenderingView extends GridLayout{

    private Context mContext;


    public TableRenderingView(Context context){
        super(context);
        mContext = context; 
        setUseDefaultMargins(true);
        setAlignmentMode(ALIGN_BOUNDS);
        setRowOrderPreserved(false);        
    }
...

}

当我尝试在LinearLayout

中动态添加此扩展类时发生异常
TableRenderingView trv = new TableRenderingView(context);
                View v = trv.populate(gridRowData);
                LinearLayout ll = ((LinearLayout) tableContainerView.getChildAt(0));
                ll.removeAllViews();
                ll.addView(v);

3 个答案:

答案 0 :(得分:7)

所以这发生了,因为我的项目所指的android-support-v4库比最新的可用库更旧。由于我们必须手动将android-support-v4.jar库复制到我们的项目/ libs文件夹中,无论你更新sdk多少次,它都会产生一点影响,直到我们手动覆盖支持库.jar那里在项目文件夹中。

我哪里出错了?我的印象是我的项目目录中有最新的android-support-v4.jar。由于我所有尝试通过更改构建路径和顺序来解决此问题都失败了,作为最后的手段我在/ extras / android / support / v4 / android中覆盖了项目/ libs中的android-support-v4.jar - 支持 - v4.jar。瞧它开始工作了。

这就是我将support-v7 gridlayout添加到我的项目中的方式

  1. 复制' gridlayout'来自' \ extras \ android \ support \ v7'的文件夹到你的android项目所在的位置
  2. 将步骤1中复制的库作为android-library导入工作区
  3. 在Android应用程序
  4. 上引用step2中导入的库

    注意:

    1. 完成上述步骤后,检查' android-support-v7-gridlayour.jar'包含在' Android依赖关系'在你的android应用程序项目下。
    2. 检查为' gridlayout'添加的新行项目在您的Android应用程序' project.properties'中。在我的情况下,添加了以下行

       android.library.reference.1=../gridlayout
      
    3. 除了上述步骤外,我没有按照提及的任何其他步骤进行here

答案 1 :(得分:1)

如果您同时使用android-support-v4android-support-v13,则还需要更新v13。这是我的情况。我已将 v4 更新为最新版本,但仍然出现错误

  

java.lang.NoSuchMethodError:android.support.v4.view.ViewCompat.hasOnClickListeners

我注意到Eclipse在控制台中的消息:“警告:在依赖项列表中找到了android-support-v4和android-support-v13。因为v13包含v4,仅使用v13”。

所以我已将 \ sdk \ extras \ android \ support \ v13 \ android-support-v13.jar 的新版本复制到我项目的libs文件夹中(已替换旧版本) )错误已经消失。

结论:我们应该立即更新我们项目中使用的所有支持库。

答案 2 :(得分:-1)

我在Xamarin Studio中遇到过类似的问题。我通过以下步骤解决了这个问题:

  1. 清除调试配置中的所有项目。
  2. 清除发布配置中的所有项目。
  3. 手动删除项目中的packages文件夹。 (删除所有Nuget包)。
  4. 重启了 电脑。
  5. 再次运行该项目。