As we all know, we can either control the view in the activity in the Activity_main.xml file or use the Java code set. () method to control it. During the learning process, it was found that the new version of ADT was different from the previous version:
After new Andriod project, mainactivity no longer inherits activity, but inherits Actionbaractivity; two. xml files are automatically generated under the Layout folder, Activity_ The Main.xml file and the Fragement_main.xml file are not the same as the previous version with only one activity_main.xml file.
Write a TextView in Fragment_main.xml, with the contents of Hello World:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= "com.example.textview.MainActivity $PlaceholderFragment "> <textview android:id=" @+id/textview " android:layout_width=" Match_ Parent " android:layout_height=" wrap_content " android:background=" #FF0000 " android:text=" @string /hello_world "/></linearlayout>
We can of course control the display of text content in the. xml file, and if you control the display of text content in the mainactivity, you can use the object that represents the control, and in the previous version of only the Activity_main.xml file, you can write the code:
public class Mainactivity extends Activity {private TextView TextView; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TextView = ( TextView) Findviewbyid (R.id.textview);//down to the TextView type, find the TextView based on the ID in the XML file,
Textview.setbackgroundcolor (Color.Blue);//Set Background color if (savedinstancestate = = null) {Getsupportfragmentmanager (). BeginTransaction (). Add (R.id.container, New Placeholderfragment ()). commit ();}}}
But in the new version, the inheritance is no longer the activity, but the actionbaractivity, and in the layout of two XML files, if written in the way described above, the program can not run, the method to solve the problem is as follows:
1. Obtain the TextView object in the Oncreateview () method in the internal static class placeholderfragment, rather than in the OnCreate () method.
2. Because it is an internal static class, it should be declared as private static TextView TextView;
Run the program, OK
Package Com.example.textview;import Android.support.v7.app.actionbaractivity;import Android.support.v7.app.actionbar;import Android.support.v4.app.fragment;import Android.graphics.Color;import Android.os.bundle;import Android.view.layoutinflater;import Android.view.menu;import Android.view.MenuItem;import Android.view.view;import Android.view.viewgroup;import Android.widget.textview;import Android.os.Build;public Class Mainactivity extends Actionbaractivity {private static TextView TextView; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//null pointer exception// TextView = (TextView) Findviewbyid (R.id.textview);//The Object//textview.setbackgroundcolor (Color.Blue) is not available here; Savedinstancestate = = null) {Getsupportfragmentmanager (). 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 isPresent.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. */public Static class Placeholderfragment extends Fragment {public placeholderfragment () {} @Overridepublic View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {View RootView = inflater.inflate (R.layout.fragment_main, container,false); TextView = (TextView) Rootview.findviewbyid (R.id.textview);//Get the object OK here!!! Textview.settext ("Hello"); return Rootview;}}}
Andriod ADT v22.6.2 using TextView control objects in Mainactivity.java in version Fragment_main.xml