Some common sense of layout in Android

Source: Internet
Author: User

In general, we will define some of the controls in the XML file to be used in the program, and the latest ADT will generate two XML files for each activity, one activity_main.xml and the other fragment_main.xml. At first I did not understand the difference between the two, in the fragment_main.xml definition of the control used in the activity, eclipse will not error. If you do not add some events for the control, such as clicking, the application will run normally. However, if you add event handling for the control, the runtime application crashes.

By looking at Logcat, the system prompts for a null pointer exception, but I can't find the code that caused the exception, only one line at a time, and finally found out that there was an error in looking for the ID of the control. But this ID I clearly defined and joined in the Fragment_main.xml. Later on the Internet to query the relevant knowledge, the Fragment_main.xml defined in the control all go to Activity_main.xml, the application will be working properly.

It is so visible that the control to be used in an activity must be defined in the corresponding activity.xml, so that it is convenient to add the control's event handling, cannot be defined in other XML files, otherwise the system will not error, but your application will crash. Hope to be like me just learn Android novice has certain help. And I don't understand why this is so? If there are seniors who know why, please comment to tell.

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.