When you build an android project, the build tools generate a class called R.java
. Because R
is a class, it will exist in a package, just like all your other classes. By default, R
will be generated in the package you've specified as the project package. For you, this should be com.android.test
.
So your package structure is actually like this, even though R is in gen/, not src/.
-com.android.test
--R.java
--com.android.test.activity
---MainActivity.java
---SplashActivity.java
--com.android.test.ui
---ActivityBase.java
In Java, you will require an import
statement if a class is not declared inside the package of the current class. So because com.android.test
is not contained in com.android.test.activity
, you will need to add import com.android.test.R;
to MainActivity and SplashActivity, in order to use R
in them.
If R
is not being generated for you, check out Developing for Android in Eclipse: R.java not regenerating. I would possibly recommend learning to use IntelliJ IDEA, as I've encountered fewer strange build issues than in eclipse, but this is completely optional.
Another possible source of error is if you accidentally imported android.R
, which is where the resources of the android SDK are referenced from. This could easily mess you up, since you could write code like R.string.foo
and think you are using com.android.test.R.string.foo
, but in reality, you are using android.R.string.foo
, which may not exist. An easy way to find the problem is to explicitly say in your code, for example:
// you were originally getting an error here
MyActivity.this.getString(R.string.foo);
// try this to get a more obvious error, or see if it fixes it
MyActivity.this.getString(com.android.test.R.string.foo);