简体   繁体   中英

“ClassNotFoundException: android.support.v7.widget.GridLayout” even if it is actually imported

I have quite an odd problem. I'm using Android Studio 2.1 on Ubuntu 15.10.

I'm trying to implement the GridLayout from v7 support libraries. I installed the Android Support Library and Android Support Repository from the SDK Manager and added the gridlayout to my apps build.gradle dependencies:

compile 'com.android.support:design:23.3.0'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:gridlayout-v7:23.3.0'

When adding it to my layouts XML file it autocompletes correctly just as if it is imported like it should.

<android.support.v7.widget.GridLayout xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:columnCount="4"
            app:rowCount="4"
            android:id="@+id/keypadButtons"
            app:alignmentMode="alignBounds">
...

But when I went to build the project to see what it looks like on a device, it for some reason fails.

When I wanted to check it out on the Design view, I see that I have a parse error in my event log which states:

java.lang.RuntimeException: java.lang.ClassNotFoundException: android.support.v7.widget.GridLayout

Edit: When also viewing the GridLayout in Design view, it even renders it as it should (in my case 4 columns and 4 rows). But gives the parse error of the GridLayout class not found and fails to show my Component Tree of other elements.

Have I missed some step when adding the library? I've used other v4 and v7 support libraries on Windows without any issues. Could it be Linux permission issues?

I have tried your code and it works fine.

In my gradle file I have:

android {
  compileSdkVersion 23
  buildToolsVersion "23.0.3"

...

defaultConfig {
     minSdkVersion 19
     targetSdkVersion 23
...


dependencies {
  compile fileTree(include: ['*.jar'], dir: 'libs')
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:appcompat-v7:23.3.0'
  compile 'com.android.support:design:23.3.0'
  compile 'com.android.support:gridlayout-v7:23.3.0'

The solution to the build problem was as simple (and somewhat embarrasing) as doing a project clean under Build -> Clean Project . That solved my issue regarding the app not building.

Now I was also stuck with the GridLayout class not being found in Design view. The solution for it I found was to invalidate the caches under File -> Invalidate Caches / Restart ( Note : This will clear your local history in Android Studio). And after a restart the GridLayout class was found correctly.

如果上述问题仍然存在,则可以将RecyclerViewGridLayoutManager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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