简体   繁体   中英

What is causing the 'cannot access ViewGroup ... class file or android.view.ViewGroup' error?

Problem: I'm not exactly sure why I'm receiving an error on my AddNote.java class. This was working before and I see the XML activity is still linked to the class. In fact, this AddNote is nearly identical to the EditNote class and activity, which is working, and the AddNote was created from a copy of the EditNote activity and class so it could be modified easily.

What I have tried: In comparison with the EditNote class and activity which is working, I cannot find anything missing. Under onCreate , the setContentView references the AddNote activity and the AddNote activty XML context ( tools.context ) is set to .AddNote . None of my other activity classes reference or import android.view.ViewGroup that I see, so I'm also confused on why I'm receiving this other than maybe it is a downstream result of something else I'm not identifying.

What I have recently done: I have recently updated the JDK, Gradle and dependency versions I was prompted to change. Below are what I'm using and snippets of what I thought were important. If something else is needed, I'm happy to provide it.

  • Intellij Idea 2021.1.1
  • JDK 11
  • Gradle 4.1.3
  • Android 29 (min 28)

The Error

C:\Users\..\researchdb\AddNote.java:32: error: cannot access ViewGroup
public class AddNote extends AppCompatActivity {
       ^
  class file for android.view.ViewGroup not found

The Class

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_note);

The Actvity XML

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorGray"
        tools:ignore="LabelFor"
        tools:context=".AddNote"   //  <-----------------------
        android:id="@+id/lvl_View_Question">

When you are shifting to latest Android Studio (Artic Fox 2020.3.1) with JDK 11 and need to support Android version 31, you can try below to make your project compile successfully:

Under build.gradle (top level folder): Use classpath "com.android.tools.build:gradle:7.0.3"

Under app\build.gradle: Use below
compileSdk 31
defaultConfig {
applicationId "com.flipcam"
minSdk 21
targetSdk 31
versionCode 7
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}

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