Android Learning-Write an apk to send SMS, pay attention to the process of layout file!!!

Source: Internet
Author: User

When I first started writing Android, I found that the output was null (NULL pointer error) after using the Findviewbyid method, which means the method was unsuccessful. Online says this is in Activity_main. xml to find the control represented by this ID, and now the ADT used in layout except Activity_main. XML also generates a fragment_main.xml. I just wrote the control in the latter layout file. Would you like to ask, what is the method used in Fragment_main.xml to find the control?

Answer: initialize TextView in Placeholderfragment's Oncreateview.

http://bbs.csdn.net/topics/390740123?page=1#post-397370237 this post is well written, thumbs up!

1  Packagecom.example.sms;2 3 Importjava.util.ArrayList;4 5 Importandroid.support.v7.app.ActionBarActivity;6 ImportAndroid.support.v7.app.ActionBar;7 Importandroid.support.v4.app.Fragment;8 ImportAndroid.telephony.SmsManager;9 ImportAndroid.os.Bundle;Ten ImportAndroid.view.LayoutInflater; One ImportAndroid.view.Menu; A ImportAndroid.view.MenuItem; - ImportAndroid.view.View; - ImportAndroid.view.ViewGroup; the ImportAndroid.widget.EditText; - ImportAndroid.widget.Toast; - ImportAndroid.os.Build; -  +  Public classMainactivityextendsactionbaractivity { -      +     StaticEditText Metnumber; A     StaticEditText metcontent; at  - @Override -     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -  in         if(Savedinstancestate = =NULL) { - Getsupportfragmentmanager (). BeginTransaction () to. Add (R.id.container,Newplaceholderfragment ()). commit (); +         } -     } the  *      $     Panax Notoginseng @Override -      Public BooleanOncreateoptionsmenu (Menu menu) { the  +         //inflate the menu; This adds items to the action bar if it is present. A getmenuinflater (). Inflate (R.menu.main, menu); the         return true; +     } -      $      $      -      -  the @Override -      Public Booleanonoptionsitemselected (MenuItem item) {Wuyi         //Handle Action Bar item clicks here. The Action Bar would the         //automatically handle clicks on the Home/up button, so long -         //As you specify a the parent activity in Androidmanifest.xml. Wu         intID =Item.getitemid (); -         if(id = =r.id.action_settings) { About             return true; $         } -         return Super. onoptionsitemselected (item); -     } -  A      +      the      -     /** $ * A placeholder fragment containing a simple view. the      */ the      Public Static classPlaceholderfragmentextendsFragment { the  the          Publicplaceholderfragment () { -         } in  the @Override the          PublicView Oncreateview (layoutinflater inflater, ViewGroup container, About Bundle savedinstancestate) { theView Rootview =inflater.inflate (R.layout.fragment_main, container, the                     false); the              +             //because it is defined in Fragment_main, it needs to be defined here. -Metnumber =(EditText) Rootview.findviewbyid (r.id.et_number); theMetcontent =(EditText) Rootview.findviewbyid (r.id.et_content);Bayi              the              the             returnRootview; -         } -     } the  the      Public voidSend (View v) { theString number =Metnumber.gettext (). toString (). Trim (); theString content =Metcontent.gettext (). toString (). Trim (); -          the     if(". Equals (number) | |" ". Equals (content)) { theToast.maketext ( This, R.string.error_message, Toast.length_long). Show ();//add show to show it the}Else{94         //This is the way to send a regular text message the         //Smsmanager.getdefault (). Sendtextmessage (number, NULL, content, NULL, NULL);//requires authorization, please note that adding the         //Toast.maketext (This,r.string.success_message, Toast.length_long). Show (); theSmsmanager Smsmanager =Smsmanager.getdefault ();98arraylist<string> messages = smsmanager.dividemessage (content);//ArrayList is a collection of types that are of type string, which is added according to Dividemessage's supplemental instructions . About          for(String message:messages) { -Smsmanager.sendtextmessage (number,NULL, Message,NULL,NULL);101             102         }103         104     } the}}

In the past, when there is no fragment_main.xml, in the OnCreate () method can be casually write code, now after testing, or in the fragment layout, convenient to drag, write code time to find the Oncreateview () method, Write in it OK ...

Another harvest is that the apk involved in the flow or spend money, need authorization, directly within the menifest authorization can.

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.