问题
I cannot seem to change the text in a textview that is in an fragment from my fragmentActivity. Additionally I have tried frag.getView().findViewById
which while I would think is the right way to do it, returns null and throws a nullpointer exception.
If I write methods in the Fragment itself to set the text and use getView(), I also get nullpointer exceptions.
Also of note, the first fragment loaded, called by the fragment:name, loads and populates text with this same code. It only does not work after FragmentTransaction.replace()
has been called
The code below does not throw any exceptions, however the text remains blank.
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);
}
}
回答1:
When querying the main View or any sub Views of a Fragment, you must wait until Fragment.onCreateView() has been called.
It seems odd that the parent Activity is responsible for modifying layout components of Fragments. One of the main advantages of using Fragments is that they can be self contained and reused throughout your application.
Perhaps a more viable approach would be to override onActivityCreated() or onStart() of your Fragments (which are called after onCreateView()), and have them populate their TextViews there.
回答2:
A working example: (this is edittext, not textview, but works on same principle)
You have to use your current Fragment View's name for it to work:
In this Example my editText dynamically updates from my database:
My View's name is rootView (I made it public so that my method can access it):
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(); }
My fetchdata() method looks like this:
I am dynamically updating my edittext from my database:
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();
}
}
来源:https://stackoverflow.com/questions/13774321/cannot-change-fragment-textview-with-settext