What is the difference between @ + id/and android: id in android development?

Source: Internet
Author: User

Today, I encountered a title Question: @ + id/what is the difference between android: id usage?

First, check the Code:

 
         
          
  
 
In the above Code, an exception occurs when the simulator runs: Java. lang. RuntimeException: Your content must have a ListView whose idAttribute is 'android. R. Id. List.

Next, let's look at the code in the Activity:

Public class ListViewActivity extends ListActivity {@ Overridedprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // if the following layout file is used, you must assume that the ListView is in this file and the id must be @ android: id/list // otherwise, the Your content must have a ListView whose id attribute is 'android. r. id. list "error // fixed usage. If the following layout is not set, the default layout is automatically used. You do not need to define listview setContentView (R. layout. listview );}}


Note that the ListActivity is inherited, not the original Activity.
ListActivity is an Activity bound to a data source and used to display this string of data.
ListActivity has a listview object to bind and display data sources,
It is usually an array or a cursor with query results
ListActivity itself has a default layout, which contains a full-screen list.
If you use the default layout, you must comment out the setContentView () Sentence in onCreate.
However, if you want to customize your own layout, you can create your own layout file and call
SetContenttView () to specify the layout .,
Note that you must have a ListView with the id "@ android: id/list" in your own layout, as shown in the XML code above.
Correct code: Modify android: id = "@ id/android: list" ------ modify -----> android: id = "@ android: id/list ", in this way, the simulator can run.


The following describes the usage of Android: id = "@.../..." x by querying the android API documentation:
In the Android system, components must be represented by an int value, which is the id attribute value in the component tag. The id attribute only accepts the value of the resource type, that is, the value that must start with @, for example, @ id/result, @ + id/user. If "+" is used after @, it indicates that after a layout file is modified and saved, the system will automatically generate the corresponding int type variable in the R. java file. The variable name is the value after "/". For example, @ + id/user will be in the R. generate int user = value in the java file (value is a hexadecimal number, for example, in R. public static final int user = 0x7f030003;) in java ;).

If the user already has a variable with the same name in R. java, the new variable will not be generated, and the component will use the value of this existing variable.
If the @ + id/name form is used, the component uses the value of the variable as the identifier when the variable name exists in R. java. If the variable does not exist, add a new variable and assign the corresponding value to the variable (no duplicates ).
In the Android system, since the component id attribute is a resource id, you can naturally set any existing resource id value, for example, @ drawable/icon, @ string/OK, @ + string/you, etc. Of course, you can also set the existing resource id in the android system. For example, what does android mean by @ id/android: list, this android is the system's R class (in R. the package in the java file.

The difference between @ + id/and android: id may not be very accurate for beginners, but it is only a process of recording personal learning.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.