所以我今天看了一个教程,里面的创建者使用的是Android Studio 3.x.x的最早版本,我发现这行代码
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
不适用于我的Android Studio版本(3.5.1)。
在谷歌上快速搜索后,我发现我不得不写
androidx.gridlayout.widget.GridLayout gridLayout = findViewById(R.id.gridLayout);
而不是。
谁能解释一下为什么新版本的代码会变得更复杂,为什么旧的代码不能工作?另外,为什么用于声明其他视图的代码仍然相同
(例如Button playAgainButton = (Button) findViewById(R.id.playAgainButton);
)?
这种不一致从何而来?
发布于 2019-10-18 14:51:44
这是因为他们使用的是Appcompat库的Androidx实例。为了迁移到AndroidX,您必须将Gradle中的依赖项更改为:
implementation 'androidx.appcompat:appcompat:1.0.0'
因此,您可以将GridLayout声明为:
androidx.gridlayout.widget.GridLayout gridLayout = findViewById(R.id.gridLayout);
https://stackoverflow.com/questions/58446496
复制