Reputation: 308
I am simply trying to set a few TextViews when the fragment is created simply first checking to make sure my sharedPrefrences have or have not been set.
My Fragment code looks like this:
@SuppressLint("ValidFragment")
public class CardBackFragment extends Fragment {
public CardBackFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
backView = inflater.inflate(R.layout.postcardback, container, false);
TextView to = (TextView) findViewById(R.id.backToLabel);
TextView address = (TextView) findViewById(R.id.backAddressLabel);
TextView city = (TextView) findViewById(R.id.cityLabel);
TextView state = (TextView) findViewById(R.id.stateLabel);
TextView zip = (TextView) findViewById(R.id.zipLabel);
TextView country = (TextView) findViewById(R.id.countryLabel);
postcard = getApplicationContext().getSharedPreferences(postcardconst, 0);
if(savedInstanceState != null){
if(!getFreshValueForBackView()){
postcard = getApplicationContext().getSharedPreferences(postcardconst, 0);
toString = (postcard.getString(toConst, null));
addressString = (postcard.getString(address1Const, null));
cityString = (postcard.getString(cityConst, null));
stateString = (postcard.getString(stateConst, null));
zipString = (postcard.getString(zipcodeConst, null));
countryString = (postcard.getString(countryConst, null));
//setText
to.setText(toString);
address.setText(addressString);
city.setText(cityString);
state.setText(stateString);
zip.setText(zipString);
country.setText(countryString);
}else{
toString = ("add recipiants address");
addressString = ("");
cityString = ("");
stateString = ("");
zipString = ("");
countryString = ("");
//setText
to.setText(toString);
address.setText(addressString);
city.setText(cityString);
state.setText(stateString);
zip.setText(zipString);
country.setText(countryString);
}
}else if(!getFreshValueForBackView()){
postcard = getApplicationContext().getSharedPreferences(postcardconst, 0);
toString = (postcard.getString(toConst, null));
addressString = (postcard.getString(address1Const, null));
cityString = (postcard.getString(cityConst, null));
stateString = (postcard.getString(stateConst, null));
zipString = (postcard.getString(zipcodeConst, null));
countryString = (postcard.getString(countryConst, null));
//setText
to.setText(toString);
address.setText(addressString);
city.setText(cityString);
state.setText(stateString);
zip.setText(zipString);
country.setText(countryString);
}else{
toString = ("add recipiants address");
addressString = ("");
cityString = ("");
stateString = ("");
zipString = ("");
countryString = ("");
}
return backView;
}
}
This keeps giving me a NPE(Null Pointer Exception) everywhere i try and setText().
Why would this not work and how can i fix it? I replicate something extremely similar on another fragment to get and save a Bitmap to ImageView.
Upvotes: 0
Views: 138
Reputation: 154
you are calling it from create view in this case finview should be used with inflated xml view use backView.findViewById(....) this ill start returning you the textview object
Upvotes: 1