Use of GridLayout from support library leads to 'NoSuchMethodError: android.support.v4.view.ViewCompat.getLayoutDirection'

馋奶兔 提交于 2019-12-10 17:11:31

问题


I am using GridLayout in my app and it works perfectly. Now I have to support android 3.xx as well, so I included android-support-library-v7. I just changed the import statements for GridLayout and LayoutParams at all places. I can build the project now but when I run the app, it crashes with NoSuchMethodError exception.

I followed these steps to include the support library

Here is the 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

EDIT: code

public class TableRenderingView extends GridLayout{

    private Context mContext;


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

}

Exception occurs when I try to add this extended class dynamically inside a LinearLayout

TableRenderingView trv = new TableRenderingView(context);
                View v = trv.populate(gridRowData);
                LinearLayout ll = ((LinearLayout) tableContainerView.getChildAt(0));
                ll.removeAllViews();
                ll.addView(v);

回答1:


So this was happening because the android-support-v4 library that my project was referring to was older than the latest available. Since we have to copy android-support-v4.jar library manually into our project's /libs folder, no matter how many times you update the sdk, it will have little impact until we manually overwrite support library .jar thats there in the project folder.

Where I went wrong? I was under the impression that I have the latest android-support-v4.jar in my project directory. Since all my attempts to solve this issue by changing the build path and order failed, as a final resort I overwritted my android-support-v4.jar in the project /libs with one in the /extras/android/support/v4/android-support-v4.jar. Voila it started working.

And this is how I added support-v7 gridlayout into my project

  1. Copy 'gridlayout' folder from '\extras\android\support\v7' to location where your android project is located
  2. Import the library copied in step1 as android-library into workspace
  3. Reference the imported library from step2 on your android application

Note:

  1. After the above steps are performed check 'android-support-v7-gridlayour.jar' is included under 'Android Dependencies' under your android application project.
  2. Check a new line is added for 'gridlayout' project in your android application's 'project.properties'. In my case the below line was added

     android.library.reference.1=../gridlayout
    
  3. Apart from the above mentioned steps I did not follow any other steps mentioned here




回答2:


If you are using both android-support-v4 and android-support-v13 you need also update v13. This was my case. I have updated v4 to the latest version but still was getting the error:

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

I have noticed Eclipse's message in console: "WARNING: Found both android-support-v4 and android-support-v13 in the dependency list. Because v13 includes v4, using only v13".

So I have copied new version of the \sdk\extras\android\support\v13\android-support-v13.jar to my project's libs folder (have replaced old version) and the error has gone.

Conclusion: we should probably update all support libraries are used in our projects at once.




回答3:


I had a similar problem in Xamarin Studio. I resolved it by doing these steps:

  1. Clean all Projects in Debug Configuration.
  2. Clean all Projects in Release Configuration.
  3. Manually deleted the packages folder in the project. (Deleting all Nuget packages).
  4. Rebooted the computer.
  5. Run the project again.


来源:https://stackoverflow.com/questions/22681691/use-of-gridlayout-from-support-library-leads-to-nosuchmethoderror-android-supp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!