0

I have been practicing in Android Studio as per the android app development free course in UDACITY. And wherever there is a R.menu or R. stuff it cannot be resolved

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.R;

public class MainActivity extends AppCompatActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}
}

Plz help how to solve this Issue.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Ronit
  • 11
  • 1
  • 1
  • 4
  • Clean and rebuild. This is a common issue. – Vucko Jul 08 '16 at 08:42
  • 1
    Possible duplicate of [R cannot be resolved - Android error](http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error) – Vucko Jul 08 '16 at 08:42

2 Answers2

7
  1. Clean and rebuild your project by going to Build>Clean Project.
  2. Go to File>Invalidate Caches/Restart.
  3. Make sure you built your project, and make sure there aren't any errors (red lines) in the res directory.
Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117
1

remove import android.R; clean and rebuild it. if it still doesn't work try this

import com.example.android.courtcounter.R;

Check you AndroidManifest.xml, check the package name there. use that as import .R; in code.

prGD
  • 1,501
  • 2
  • 12
  • 22
  • Removing `import android.R` will actually CAUSE the error. -1 – Ali Bdeir Jul 08 '16 at 08:58
  • he has to import his R file path not android.R that why I asked to check with import com.example.android.courtcounter.R; – prGD Jul 08 '16 at 09:02
  • He doesn't even have to import that. Android Studio does everything for him. I don't have any import for `R`, and AS does it for me. It doesn't really matter. – Ali Bdeir Jul 08 '16 at 09:10
  • that's what i told first remove import android.R; and clean and rebuild it. Please read it fully – prGD Jul 08 '16 at 09:17
  • Ah, oops. Sorry. I'll see if I can remove the downvote – Ali Bdeir Jul 08 '16 at 09:18
  • Still not working. After even cleaning and then rebuilding it is showing error 'C:\Users\SAMADDAR\Desktop\Programming\Java\BlueJ\AndroidStudioProjects\CourtCounter\app\src\main\java\com\example\android\courtcounter\MainActivity.java Error:(21, 36) error: cannot find symbol variable menu Error:(33, 23) error: cannot find symbol variable action_settings :app:compileDebugJavaWithJavac FAILED Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. Information:BUILD FAILED' – Ronit Jul 08 '16 at 12:08
  • And then when i include that import.com.example.android.courtcounter.R; It is saying unused import statement – Ronit Jul 08 '16 at 12:11
  • And in the R.layout.activity_main it is not causing any error. But in R.menu.menu_main and R.id.action_settings it is not able to resolve **menu** and **action_settings** – Ronit Jul 08 '16 at 12:17
  • How is your folder structure? can you share your menu xml file – prGD Jul 08 '16 at 18:01
  • also check your menu.xml in /res/menu/ folder – prGD Jul 08 '16 at 18:11