Android uses include to invoke internal component methods _android

Source: Internet
Author: User
Tags stub

The examples in this article describe how Android uses include to invoke internal components. Share to everyone for your reference. Specifically as follows:

Example one:

Sublayout.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" 
  Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Wrap_content " android:background= "#505050" > <textview android:layout_width= "fill_parent android:layout_height=" Wrap_ Content "android:text=" Sublayout "/> <button android:id=" @+id/mybutton "android:layout_width=" fill_parent  "Android:layout_height=" wrap_content "android:text=" A button "/> </LinearLayout> mail.xml <?xml 
  Version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <T Extview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@string/hello" > <include android:id= "@+id/maIn1 "layout=" @layout/sublayout "/> <include android:id=" @+id/main2 "layout=" @layout/sublayout "/> <Button 
  Android:id= "@+id/startanotheractivity" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" 

 android:text= "Start Another activity"/> </LinearLayout>

How to invoke the component included in the component.

Package com. 
Androidincludelayout; 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.widget.Button; 
Import Android.widget.Toast; 
  The public class Androidincludelayout extends activity {/** called the ' when ' is the ' The activity ' is the ' the '---' @Override 
    public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    Setcontentview (R.layout.main); 
    View SUBLAYOUT1 = (view) Findviewbyid (r.id.main1); 
    View SubLayout2 = (view) Findviewbyid (r.id.main2); 
    Button mybutton_main1 = (button) Sublayout1.findviewbyid (R.id.mybutton); 
    Button mybutton_main2 = (button) Sublayout2.findviewbyid (R.id.mybutton); 
    Button startanotheractivity = (button) Findviewbyid (r.id.startanotheractivity); 
  Startanotheractivity.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View arg0) { TODO auto-generated Method Stub Intent Intent = new IntenT (); 
       Intent.setclass (Androidincludelayout.this, Anotheractivity.class); 
  StartActivity (Intent); 
    }}); Mybutton_main1.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View arg0) {//TOD 
  O auto-generated Method Stub toast.maketext (androidincludelayout.this, "button 1 pressed", Toast.length_long). Show (); 
    }}); Mybutton_main2.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View arg0) {//TOD 
  O auto-generated Method Stub toast.maketext (androidincludelayout.this, "button 2 pressed", Toast.length_long). Show (); 
  }}); } 
}

But if the included XML is

Sublayout.xml

<?xml version= "1.0" encoding= "Utf-8"?> <merge xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
<textview 
  android:layout_width=" fill_parent " 
  android:layout_height=" Wrap_content " 
  android:text= "sublayout" 
  /> 
<button 
android:id= 
  "@+id/mybutton" android:layout_width = "Fill_parent" 
  android:layout_height= "wrap_content" 
  android:text= "A button" 
  /> 
</ Merge> 

The above method will not be implemented and a null pointer will be reported.
Because after using the merge, the import is equivalent to the current view of the component, so direct findviewbyid on it.

In this case ... You can solve the problem of include multiple times with the same layout

I hope this article will help you with your Android program.

Related Article

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.