Android uses fragment, cannot get fragment internal controls, Findviewbyid () result is null--has been resolved

Source: Internet
Author: User

The program is easy. I haven't done it for a long time. Depressed....

。。。。

Consult in the Forum and find out the answer eventually.

Descriptive narrative:

A activity:mainactivity. The interior is a fragment:fragmenta. There are textview in Fragmenta.

Problem: No matter what the Fragmenta internal TextView, the return value is always null

Detailed description of the narrative:

Mainactivity's Layout:activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?

><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= " Horizontal " android:layout_width=" match_parent " android:layout_height=" match_parent "> < Fragment Android:name= "Com.fragmentexercise.FragmentA" android:id= "@+id/fragment_a" android:layout_ Width= "Match_parent" android:layout_height= "Match_parent"/></linearlayout>

Fragmenta's Layout:fragment_a.xml

<?

XML version= "1.0" encoding= "Utf-8"? ><textview xmlns:android= "Http://schemas.android.com/apk/res/android" android:id= "@+id/textview1" android:layout_width= "match_parent" android:layout_height= "Match_ Parent " android:padding=" 16DP " android:textsize=" 18sp " android:text=" Initialize text "/>


Mainactivity.java

public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);}

Fragmenta.java

public class Fragmenta extends Fragment {@Overridepublic View oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {return inflater.inflate (R.layout.fragment_a, container, false);} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {super.onactivitycreated (savedinstancestate);// TextView textView1 = (TextView) GetView (). Findviewbyid (R.id.textview1);//textview1.settext ("Change");}

Detailed questions:

In the fragmenta.onactivitycreated ().
TextView textView1 = (TextView) GetView (). Findviewbyid (R.id.textview1); The resulting is always null.



In Fragment.oncreateview (),

Public View Oncreateview (Layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {Second number of references Container is always a null value


In Mainactivity, Findviewbyid (R.id.textview1), the same is a null value.


What is the problem??????????


===============================================================================================================

Reason


Let the activity inherit android.support.v4.app.FragmentActivity, let fragment inherit android.support.v4.app.Fragment will be able to

Such Getactivity () Findviewbyid (R.ID.TEXTVIEW1) will be able to get this textview in OnStart methods such as Fragmenta.


If you inherit  android.app.fragment directly, you can't. Maybe it's an Android bug.

In fact I did it in accordance with the example of trainning on developer.android.com. The example is to be compatible with android3.0 's system, inheriting the android.support.v4.app.Fragment. Official sample execution is normal.


My cell phone is Android4.1 and can support fragment. So I inherited the adnroid.app.Fragment directly. The result is not successful.

I can't believe it's the reason.

Android uses fragment, cannot get fragment internal controls, Findviewbyid () result is null--has been resolved

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.