2

This is my project stricture :

-com.android.test
--com.android.test.activity
---MainActivity.java
---SplashActivity.java  
--com.android.test.ui
---ActivityBase.java

I created all activity in com.android.test.activity package and all activities extended from Base activity in com.android.test.ui package
But i don't know , when i creating activities on com.android.test.activity i see .R error in my Eclips , but when i creating activity on com.android.test package ,no need for importing the .R class.
I'm very beginner , please help

  • R.java is the dynamically generated class, created during build process to dynamically identify all assets (from strings to android widgets to layouts), for usage in java classes in Android app. **Note** this R.java is Android specific (though you may be able to duplicate it for other platforms, its very convenient) so it doesn't have much to do with Java language constructs. – Tushar Gogna Aug 12 '14 at 12:52
  • How can solve this problem?any setting in eclips? –  Aug 12 '14 at 13:02
  • What is your Android package name? – Code-Apprentice Aug 12 '14 at 23:52

1 Answers1

1

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);
Community
  • 1
  • 1
Trevor Siemens
  • 629
  • 5
  • 10