无法使用setText更改片段TextView

时间:2012-12-08 03:57:36

标签: android android-fragments actionbarsherlock

我似乎无法更改fragmentActivity片段中textview中的文本。另外我尝试了frag.getView().findViewById虽然我认为这是正确的方法,但返回null并抛出一个nullpointer异常。

如果我在Fragment中编写方法来设置文本并使用getView(),我也会得到nullpointer异常。

另外值得注意的是,加载的第一个片段(由fragment:name调用)使用相同的代码加载和填充文本。它仅在调用FragmentTransaction.replace()后才起作用

下面的代码不会抛出任何异常,但文本仍为空白。

public void onTabSelected(Tab tab, FragmentTransaction ft) {
    if(tab.getTag().equals("info")) {
        DisplayEventInfoFragment frag = new DisplayEventInfoFragment();
        ft.replace(R.id.detailsfragment, frag);
        final TextView mTimeView = (TextView)findViewById(R.id.ievent_title);
        final TextView mVenueView = (TextView)findViewById(R.id.ievent_venue);
        final TextView mDescView = (TextView)findViewById(R.id.ievent_description);
        mTimeView.setText(thisEvent.get("theTime")+" - "+thisEvent.get("endTime"));
        mVenueView.setText(thisEvent.get("venue"));
        mDescView.setText(thisEvent.get("description"));

    } else if(tab.getTag().equals("location")) {
        //DisplayEventMapFragment frag = new DisplayEventMapFragment();
        //ft.replace(R.id.detailsfragment, frag);
    }

}

2 个答案:

答案 0 :(得分:6)

查询主视图或片段的任何子视图时,必须等到调用Fragment.onCreateView()。

父Activity调用Fragments的布局组件是很奇怪的。使用Fragments的一个主要优点是它们可以自包含并在整个应用程序中重用。

或许更可行的方法是覆盖片段的onActivityCreated()或onStart()(在onCreateView()之后调用),并让它们在那里填充TextView。

答案 1 :(得分:0)

一个工作示例:(这是edittext,而不是textview,但工作原理相同)

您必须使用当前的片段视图名称才能工作:

在此示例中,我的editText从我的数据库中动态更新:

我的视图名称是rootView(我将其公开,以便我的方法可以访问它):

public class FragmentPersonalD extends Fragment {

EditText PDADDUNIQUECODE;
EditText PDADDIDNR;
EditText PDADDNAME;
EditText PDADDSURNAME;
EditText PDADDEMAIL;
EditText PDADDAGE;
EditText PDADDGENDER;
EditText PDADDCELLNR;
EditText PDADDSERVICEPROVIDER;
EditText PDADDHOMENR;
EditText PDADDWORKNR;
EditText PDADDSTREETNAME,PDADDSUBURB,PDADDCITY, PDADDPROVINCE,PDADDPOSTALCODE;
EditText PDADDPOSTADDR;
EditText PDADDWORKADDR;

String setid;
String uniquecode;
String identitynr;
String Name;
String Surname;
String Email;
String Age;
String Gender;
String Cellnr;
String ServiceProvider;
String Homenr;
String Worknr;
String Streetname,Suburb,City,Province,PostalCode;
String PostAddr;
String WorkAddr;

View rootView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_details_personal,
            container, false);
    fetchdata(); }

我的fetchdata()方法如下所示:

我正在从我的数据库中动态更新我的edittext:

public void fetchdata()
{
    DBAdapter myDb = new DBAdapter(getActivity());
    myDb.open();

    int rows = myDb.CountrowsPersonalDetails();

    if(rows != 0)
    {
        Cursor cursor = myDb.getAllRowsPersonalDetails();
        if (cursor.moveToFirst()) {

            identitynr = cursor.getString(DBAdapter.COL_pdIDNR);
            Name = cursor.getString(DBAdapter.COL_pdNAME);
            Surname = cursor.getString(DBAdapter.COL_pdSURNAME);
            Email = cursor.getString(DBAdapter.COL_pdEMAIL);

            Age = cursor.getString(DBAdapter.COL_pdAGE);
            Gender = cursor.getString(DBAdapter.COL_pdGENDER);
            Cellnr = cursor.getString(DBAdapter.COL_pdCELLNR);
            ServiceProvider = cursor.getString(DBAdapter.COL_pdSERVICEPROVIDER);

            Homenr = cursor.getString(DBAdapter.COL_pdHOMENR);
            Worknr = cursor.getString(DBAdapter.COL_pdWORKNR);
            Streetname = cursor.getString(DBAdapter.COL_pdSTREETADDRESS);
            Suburb = cursor.getString(DBAdapter.COL_pdSUBURB);
            City = cursor.getString(DBAdapter.COL_pdCITY);
            Province = cursor.getString(DBAdapter.COL_pdPROVINCE);
            PostalCode = cursor.getString(DBAdapter.COL_pdPOSTALCODE);

            PostAddr = cursor.getString(DBAdapter.COL_pdPOSTALADDRESS);
            WorkAddr = cursor.getString(DBAdapter.COL_pdWORKADDRESS);
            }

        PDADDSTREETNAME = (EditText)rootView.findViewById(R.id.PDADDSTREETNAME);
        PDADDSTREETNAME.setText(Streetname);

        PDADDSUBURB = (EditText)rootView.findViewById(R.id.PDADDSUBURB);
        PDADDSUBURB.setText(Suburb);

        PDADDCITY = (EditText)rootView.findViewById(R.id.PDADDCITY);
        PDADDCITY.setText(City);

        PDADDPROVINCE = (EditText)rootView.findViewById(R.id.PDADDPROVINCE);
        PDADDPROVINCE.setText(Province);

        PDADDPOSTALCODE = (EditText)rootView.findViewById(R.id.PDADDPOSTALCODE);
        PDADDPOSTALCODE.setText(PostalCode);


        PDADDIDNR = (EditText)rootView.findViewById(R.id.PDADDID);
        PDADDIDNR.setText(identitynr);

        PDADDNAME = (EditText)rootView.findViewById(R.id.PDADDNAME);
        PDADDNAME.setText(Name);

        PDADDSURNAME = (EditText)rootView.findViewById(R.id.PDADDSURNAME);
        PDADDSURNAME.setText(Surname);

        PDADDEMAIL = (EditText)rootView.findViewById(R.id.PDADDEMAIL);
        PDADDEMAIL.setText(Email);

        PDADDAGE = (EditText)rootView.findViewById(R.id.PDADDAGE);
        PDADDAGE.setText(Age);

        PDADDGENDER = (EditText)rootView.findViewById(R.id.PDADDGENDER);
        PDADDGENDER.setText(Gender);

        PDADDCELLNR = (EditText)rootView.findViewById(R.id.PDADDCELL);
        PDADDCELLNR.setText(Cellnr);

        PDADDHOMENR = (EditText)rootView.findViewById(R.id.PDADDHOMENR);
        PDADDHOMENR.setText(Homenr);

        PDADDWORKNR = (EditText)rootView.findViewById(R.id.PDADDWORKNR);
        PDADDWORKNR.setText(Worknr);

        //PDADDPHYSICALADDR = (EditText)rootView.findViewById(R.id.PDADDPHYSICAL);
        //PDADDPHYSICALADDR.setText(PhysicalAddr);

        PDADDPOSTADDR = (EditText)rootView.findViewById(R.id.PDADDPOSTADDR);
        PDADDPOSTADDR.setText(PostAddr);

        PDADDWORKADDR = (EditText)rootView.findViewById(R.id.PDADDWORKADDR);
        PDADDWORKADDR.setText(WorkAddr);
        myDb.close();
    }
    else
    {
        myDb.close();
    }
}