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

6.8k views Asked by At

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

There are 2 answers

0
koushick On

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

In build.gradle (top level folder):

Use

classpath "com.android.tools.build:gradle:7.0.3"

In app\build.gradle:

Use

compileSdk 31 
defaultConfig { 
    applicationId "com.your.applicationid" 
    minSdk 21 
    targetSdk 31 
    versionCode 7 
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 
 }

 compileOptions { 
    sourceCompatibility JavaVersion.VERSION_11 
    targetCompatibility JavaVersion.VERSION_11 
}
0
Mohsen Hrt On

I changed compileOptions to

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

and it's worked well.