1

我似乎无法更改片段活动中片段中的文本视图中的文本。此外,我尝试frag.getView().findViewById了虽然我认为是正确的方法,但返回 null 并引发 nullpointer 异常。

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

同样值得注意的是,第一个加载的片段,由片段名称调用,使用相同的代码加载和填充文本。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);
    }

}
4

2 回答 2

6

当查询一个 Fragment 的主视图或任何子视图时,你必须等到 Fragment.onCreateView() 被调用。

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

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

于 2012-12-10T05:16:39.677 回答
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();
    }
}
于 2014-09-01T10:56:22.477 回答