"ClassNotFoundException: android.support.v7.widget.GridLayout"即使是真的进口

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

我有一个很奇怪的问题。我在 Ubuntu 15.10.

上使用 Android Studio 2.1

我正在尝试从 v7 支持库中实现 GridLayout。我从 SDK 管理器安装了 Android Support LibraryAndroid Support Repository,并将网格布局添加到我的应用 build.gradle 依赖项:

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'

将它添加到我的布局 XML 文件时,它会正确自动完成,就像它应该被导入一样。

<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">
...

但是当我去构建项目以查看它在设备上的样子时,由于某种原因它失败了。

当我想在“设计”视图中查看它时,我发现我的事件日志中有一个解析错误,其中指出:

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

编辑: 当还在设计视图中查看 GridLayout 时,它甚至会按应有的方式呈现它(在我的例子中是 4 列和 4 行)。但是给出了 GridLayout class 未找到的解析错误,并且无法显示我的其他元素的组件树。

我是不是在添加库的时候漏掉了一些步骤?我在 Windows 上使用了其他 v4 和 v7 支持库,没有任何问题。会不会是 Linux 权限问题?

我试过你的代码,它工作正常。

在我的 gradle 文件中我有:

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'

构建问题的解决方案就像在 Build -> Clean Project 下清理项目一样简单(而且有点尴尬)。这解决了我关于应用程序未构建的问题。

现在我也遇到了在设计视图中找不到 GridLayout class 的问题。我找到的解决方案是使 File -> Invalidate Caches / Restart 下的缓存无效(注意:这将清除 Android Studio 中的本地历史记录)。重新启动后,正确找到了 GridLayout class。

如果上述问题仍然存在,您可以改用 RecyclerViewGridLayoutManager