-1

You see, I am beginning to working with Android development, and confused. For the sake of not making too much misspelling, I would like to just ask the questions:

  1. There are many R.java in different packages as you can see here, so what is the differences between these R.java files? If they are the same, then what is the purpose of keeping these R.java files?

  2. Does there exist an incredible relationship between these R.java files with that one in the project, which can be dynamically self-edited?

  3. As I creating my "Hello" Activity which extends class AppCompatActivity in android.support.v7.app package, and beginning to say hello to the world, I came across my problem: does this mean from this very point on, my project has nothing to do with the package known as android.app?

Most important, any help and guide would be appreciated from the bottom of my heart, and please keep this in mind: I'm a clumsy beginner, but humble, and very hungry to learn.

Jonik
  • 80,077
  • 70
  • 264
  • 372
李鸿章
  • 343
  • 1
  • 2
  • 10
  • 3
    Hello and welcome to SO. You should check: http://stackoverflow.com/help/how-to-ask and http://stackoverflow.com/help/on-topic – Mariano Zorrilla Sep 18 '15 at 13:34
  • It associates your resources to unique ids. – Phantômaxx Sep 18 '15 at 13:41
  • Related reading: http://stackoverflow.com/questions/10004906/what-is-the-concept-behind-r-java and http://stackoverflow.com/questions/11698808/in-android-applications-what-is-the-role-of-r-java – Jonik Sep 18 '15 at 13:48
  • Oh, and from official developer docs: [Accessing Resources](http://developer.android.com/guide/topics/resources/accessing-resources.html) – Jonik Sep 18 '15 at 13:53

2 Answers2

2

There's two main uses of R.java

1) You reference your own resources such as layouts, strings, ids etc, all those resources, you yourself created using constants from yourpackage.R.java file

That is, your string String mystring = getString(R.string.mystr) will refer to <string name="mystr>blalbabla</string> in your strings.xml file

2) Exactly the same, you can refer existing android resources by means of android's R.java such as:

android.R.layout.simple_list_item_1 

This android.R.layout.simple_list_item_1 is a layout android already has for you (which is a simple list item, with a text in it only)

Other libraries or submodules can have their own R.java, used the same way.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • Thank you, problems almost solved. And why this snippet could cause my App crash? – 李鸿章 Sep 18 '15 at 14:05
  • Intent intent=getIntent(); TextView textView=(TextView)this.findViewById(R.id.tvid); textView.setText(intent.getStringExtra("DATA").toString()); – 李鸿章 Sep 18 '15 at 14:06
  • Probably intent.getStringExtra("DATA") is null. You should make sure it's not null before calling toString() on it – Alexander Kulyakhtin Sep 18 '15 at 14:07
  • When I created View with code in run time instead of XML, it performed well, so in this case intent.getStringExtra("DATA") is not null. When Views are created with XML statically, does there exist some tricks to manage these Views in run time dynamically? Something like the static against the dynamic, only be one or another with no other choices.... – 李鸿章 Sep 18 '15 at 14:23
  • getIntent().getStringExtra("DATA") just takes the intent with which you started your activity and returns a value for "DATA" you previously put in there by some putXXX(). Does not have to do with view inflation – Alexander Kulyakhtin Sep 18 '15 at 14:31
0

short answer : R file is being build by your builder and you don't touch it ever it maps your resources into integers which helps you to refer them easily

sadegh saati
  • 1,168
  • 1
  • 13
  • 24
  • Thank you for answering, which means I do make sense. And how about a long answer for why the R file keep so many references to begin, even if I didn't create an id, String or some else? Or R file built by the builder must be something to do with the R files in those different packages as mentioned above. – 李鸿章 Sep 18 '15 at 13:48
  • there are some resource by default in your application before you even start coding like some colors or layouts or ... ,which couse your application to have some default refrences in R file – sadegh saati Sep 18 '15 at 13:51
  • Why should the builder do that? I mean, if there are no objects to refer to, why the builder bather to create those references by default? And if they are necessary, how to take good use of those references? – 李鸿章 Sep 18 '15 at 14:12