Troubleshoot issues with components in Fragment_main.xml files in Android

Source: Internet
Author: User

Package Com.dhy.phonedial;import Android.app.activity;import Android.app.fragment;import android.content.Intent; Import Android.net.uri;import android.os.bundle;import Android.view.layoutinflater;import Android.view.Menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.widget.edittext;public class MainActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); if (savedinstancestate = = null) {Getfragmentmanager (). BeginTransaction (). Add (R.id.container, New Placeholderfragment ()). commit ();}} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (); if (id = = r.id.action_settings) {return true;} return super.onoptionsitemselected (item);} /** * A placeholder fragment containing a simple view. A placeholder fragment contains a simple view. *//** * This internal class is the latest development tool automatically completed, its function is to load and parse the Fragment_main.xml file, you can get fragment_main. * Registered components in XML. * * @author Martin * */public static class Placeholderfragment extends Fragment {public placeholderfragment () {} @Overrid Epublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {//Get View object according to fragment _main.xml file Final View Rootview = Inflater.inflate (R.layout.fragment_main,container, false);//Get Component button from view object DIALBTN = (Button) Rootview.findviewbyid (r.id.btn_dial); System.out.println (Rootview.findviewbyid (r.id.btn_dial) + "============" + r.id.btn_dial + "===================" + DIALBTN);//Add an event for a component//Because I'm also a beginner. The problem of how to get components without using anonymous inner classes is not resolved, DIALBTN.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method stub//Same Gets the component through the View object EditText Et_number = (EditText) Rootview.findviewbyid (R.id.edit_number); String number = Et_number.gettext (). toString (); System.out.println ("================" + number), Intent Intent = new Intent (); intent.setaction (Intent.action_call); Intent.setdata (Uri.parse ("Tel:" + number)); StartActivity (intent);}); return Rootview;}}}

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.