2

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">
svstackoverflow
  • 665
  • 6
  • 19
  • 1
    Does this help you? [Android Studio - build problems after switching JDK to version 1.10](https://stackoverflow.com/questions/57017645/android-studio-build-problems-after-switching-jdk-to-version-1-10) – sozsoy May 22 '21 at 01:35
  • @Sozsoy After trying the Intellj version suggestedi in that link I now get an error that my project cannot be opened in Intellij or Android Studio and to retry with version 2020.3.1 but I'm on a newer version 2021.1.1. So I'm not sure what to do from here? Thoughts? – svstackoverflow May 24 '21 at 12:49
  • So, Sozsoy's suggestion did solve this issue, but it revealed an issue in both the Intellij IDE and the Android Studio -- I know one in the same, but the errors were different. Errors: Android Stuido -> Invalid injected android support version; Intellij Idea -> Android support plugin for Intellij cannot open this project. – svstackoverflow May 26 '21 at 11:22

2 Answers2

2

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 
}
Garg
  • 2,731
  • 2
  • 36
  • 47
koushick
  • 157
  • 1
  • 8
1

I changed compileOptions to

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

and it's worked well.

Mohsen Hrt
  • 263
  • 2
  • 9