11

I've a resource directory called "raw" in the "res" folder, but still can't access it through my code. The problem occurs when trying to declare the MediaPlayer variable.

public class MainActivity extends AppCompatActivity {

    private MediaPlayer pSong;

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

        pSong = MediaPlayer.create(this,R.raw.rando);


        Button playButton = (Button) findViewById(R.id.play);

        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"Play", Toast.LENGTH_SHORT).show();
            }
        });

        }

}
Erik A
  • 31,639
  • 12
  • 42
  • 67
HaakonFlaar
  • 387
  • 2
  • 4
  • 15
  • https://stackoverflow.com/questions/17054000/cannot-resolve-symbol-r-in-android-studio?rq=1 Have you looked here? – Michel_T. Jun 18 '18 at 13:09
  • @Michel_T. Cleaning the build solved the problem. I'm new to Android Studio. Care to tell me what this does? Also, everytime I make a new project I have to change: implementation 'com.android.support:appcompat-v7:28.0.0-alpha3' to alpha1 (instad of alpha3). Why is this and can I premanently set it to alpha1? – HaakonFlaar Jun 18 '18 at 13:14

9 Answers9

14

Android Studio 3.6+:

Click on Build -> Rebuild Project and then click File -> Sync Project with Gradle Files

Android Studio 3.5 and earlier:

Click on Build -> Rebuild Project and then click Tools -> Android -> Sync Project with Gradle Files

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
Akash Elhance
  • 304
  • 3
  • 14
  • 7
    In new version "Sync Project with Gradle Files" is under Files , not Tools. Secondly it did not work for me. Restarting Android Studio fixed the problem for me. – TheTechGuy Oct 22 '19 at 14:34
14

Rebuilding and Syncing did not work for me. Had to restart Android Studio.

Matt
  • 985
  • 2
  • 9
  • 22
6
  1. Build then Clean >> didn't solve
  2. Removing Build folder from app level >> didn't solve
  3. Removing Build folder from Project level >> didn't solve
  4. Synch project with Gradle >> didn't solve

Finally solved by restarting android studio

2

Hitting Build -> Clean Project solved the problem.

HaakonFlaar
  • 387
  • 2
  • 4
  • 15
1

Go to File -> Re-import Gradle Project It worked for me

1

In the new version, Just after writing the code, run code, it'll run adjusting the error itself.

1

Android Studio 4+: Go to File -> Invalidate Caches / Restart this will let android studio to index the raw files.

Methnani Bilel
  • 1,366
  • 4
  • 20
  • 34
0

I put music file in the raw folder in type ".m4a" and then error gone (worked)

0

Use Your Package name In front of R for Example:

com.example.app.R.raw.yourFile
Elikill58
  • 4,050
  • 24
  • 23
  • 45