Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
like image 874
Jeevan Avatar asked Mar 20 '23 02:03

Jeevan


1 Answers

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

like image 134
Jeevan Avatar answered Apr 25 '23 09:04

Jeevan