In my app, I have a profile layout where user can save some personal data.
For age, I have put a Spinner at this form in the Profile.class:
ArrayAdapter<String> adapterAge = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, ltAge);
adapterAge.setDropDownViewResource(android.R.layout.browser_link_context_header);
spinnerAge.setAdapter(adapterAge);
spinnerAge.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
**((TextView) parent.getChildAt(0)).setTextColor(getResources().getColor(R.color.itemsbackgr_black));** //Color
**((TextView) parent.getChildAt(0)).setTextSize(18);** //Size
**((TextView) parent.getChildAt(0)).setTypeface(MainActivity.font);** //Font
saveAge = (parent.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView<?> parent) {
//Do nothing.
}
});
All this code works well. My problem is when I am in this class and I change to another, if I push 'Key Back' to return to profile class my app breaks due to:
java.lang.NullPointerException
at com.example.Profile$2.onItemSelected(Profile.java:119)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
at android.widget.AdapterView.selectionChanged(AdapterView.java:879)
at android.widget.AdapterView.checkSelectionChanged(AdapterView.java:1043)
at android.widget.AdapterView.handleDataChanged(AdapterView.java:1022)
In the normal cycle of the app there is no problem, but if you go back yes.
3 lines where appears parent.getChildAt(0)
cause mistake ... why? How I can solve this?
Thanks.
this is custom spinner adapter
and set this adapter
R.layout.search_spinner_item file
Font face
Customize as per requirement