Question

Can someone help me?

public class Maths4to5Home extends Activity implements OnClickListener 
    {
        private Button button_4to5PlayGame, button_mathsInstructions, button_4to5Scores;

        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maths4to5home); //error

            button_4to5PlayGame = (Button)findViewById(R.id.button_4to5PlayGame); //error
            button_mathsInstructions = (Button)findViewById(R.id.button_mathsInstructions); //error
            button_4to5Scores = (Button)findViewById(R.id.button_4to5Scores); //error

I have included a sample of code above and highlighted where my errors are. I am being constantly told that R cannot be resolved to a variable. I have tried cleaning and rebuilding. Also I have tried importing R (as the quick fix suggests) but then I am told that for example in my setContentView() that

activity_maths4to5home cannot be resolved or is not a field

Any help would be greatly appreciated!

Was it helpful?

Solution

You need to import the correct R — specifically, the one for your project. This is based on what's in your project's manifest. If your manifest declares the project's package to be com.example.my_project then you need to import com.example.my_project.R;.

If your activity is already in the project's package, then you do not need to import R. However, you specifically need to not import android.R.

The error message may also be caused by a missing R.java. This can happen if errors in your resources prevents R.java from being generated. You should check that there are no errors in any of your resource files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top