UFO ET IT

Android 스튜디오 : Gradle : 오류 : 기호 변수를 찾을 수 없습니다.

ufoet 2021. 1. 12. 08:08
반응형

Android 스튜디오 : Gradle : 오류 : 기호 변수를 찾을 수 없습니다.


나는 내 앱에서 작업하고 있었고 자바에서 이미지를 표시하려고 할 때까지 모든 것이 정상이었습니다.

앱을 한 번 실행했는데 정상적으로 실행되면 사진이 표시되었습니다. 그 후 일부 라이브러리를 가져 오도록 요청했고 가져 왔습니다. 그 후 내 활동에 오류가 발생했습니다.

다음과 같은 오류 :

Gradle: error: cannot find symbol variable activity_main
Gradle: error: cannot find symbol variable button1
Gradle: error: cannot find symbol variable button2
Gradle: error: cannot find symbol variable textView
Gradle: error: cannot find symbol variable secondActivity

MainActivity에서 다음 라이브러리를 가져 왔습니다.

import android.R;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;

그리고 secondActivity에서는 다음과 같습니다.

import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

누구든지 이것을 고치는 방법을 알고 있습니까?

편집 : 삭제 import android.R;했고 이제 정상적으로 작동합니다.


을 가져 오면 안됩니다 android.R. 자동으로 생성되고 인식되어야합니다. 이 질문 에는 R가져 오기를 제거한 후 참조하는 오류가 발생하는 경우 유용한 팁이 많이 포함되어 있습니다 .

이러한 오류가 나타나는 경우 가져 오기를 제거한 후 몇 가지 기본 단계 :

  • 빌드를 정리 한 다음 다시 빌드하십시오.
  • XML 파일에 오류나 오타가 없는지 확인하십시오.
  • 리소스 이름이 [a-z0-9.]. 어떤 이유로 든 대문자 나 기호는 허용되지 않습니다.
  • Gradle 동기화 수행 (도구> Android> Gradle 파일과 프로젝트 동기화를 통해)

여러 맛을 사용 하는 경우 ?

-리소스 파일이 플레이버 중 하나와 메인 모두에서 선언 / 추가되지 않았는지 확인하십시오.

예 : a_layout_file.xml기호 변수를 포함하는 파일

src :

flavor1 / res / layout / (파일 없음)

flavor2 / res / 레이아웃 /a_layout_file.xml

메인 / res / layout /a_layout_file.xml

이 설정은 오류를 제공합니다. 심볼 변수를 찾을 수 없습니다. 이것은 리소스 파일이 두 플레이버 에만 있거나 메인 에만있을 수 있기 때문 입니다.


String프로젝트에서 빌드 구성 필드를 사용하는 경우 다음과 같은 경우 일 수 있습니다.

buildConfigField "String", "source", "play"

위와 같이 String을 선언하면 오류가 발생합니다. 수정 사항은 다음과 같이 변경하는 것입니다.

buildConfigField "String", "source", "\"play\""

가져온 R이 다른 모듈에서 가져온 것이 아닌지 확인하십시오. 나는 모듈에서 메인 프로젝트로 클래스를 옮겼고, R은 모듈에서 나온 것이었다.


당신이이 있는지 확인 MainActivity하고 .ScanActivity당신에 AndroidManifest.xml파일을

<activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ScanActivity">

    </activity>

변수가이를 참조하는 메서드의 범위 내에 있는지 확인하십시오. 예를 들어 클래스의 메서드에서 로컬로 textview를 정의했고 다른 메서드에서이를 참조했습니다.

다른 메서드가 정의에 액세스 할 수 있도록 textview 정의를 클래스 정의 바로 아래 메서드 외부로 이동하여 문제를 해결했습니다.


Another alternative to @TouchBoarder's answer above is that you may also have two layout files with the same name but for different api versions. You should delete the older my_file.xml file

my_file.xml
my_file.xml(v21)

Open project in android studio click file and click Invalidate Caches/Restart

ReferenceURL : https://stackoverflow.com/questions/18154499/android-studio-gradle-error-cannot-find-symbol-variable

반응형