parent.getChildAt(0) cause mistake if you go back

754 views Asked by At

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.

1

There are 1 answers

7
Anand Savjani On

this is custom spinner adapter

class MySpinnerAdapter extends ArrayAdapter<String>
{
    public MySpinnerAdapter(Context searchFragment, int resource, String[] arrayEstablishType)
    {
        super(searchFragment, resource, arrayEstablishType);
    }

    @Override
    public int getCount()
    {
        return super.getCount();
    }

    @Override
    public String getItem(int position)
    {
        return super.getItem(position);
    }

    @Override
    public long getItemId(int position)
    {
        return super.getItemId(position);
    }
}

and set this adapter

String arrayGender[] = getResources().getStringArray(R.array.array_gender);
MySpinnerAdapter arrayGenderAdapter = new MySpinnerAdapter(v.getContext(), R.layout.search_spinner_item, arrayGender);
spGender.setAdapter(arrayGenderAdapter);
spYearGroup.setOnItemSelectedListener(this);

R.layout.search_spinner_item file

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textSpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/scale_5"
    android:layout_marginRight="@dimen/scale_5"
    android:fontFamily="opensansbold.ttf"
    android:maxLines="1"
    android:padding="@dimen/scale_5"
    android:textColor="@color/color_search_dropdown_font"
    android:textSize="@dimen/txt_14"
    android:textStyle="bold" />

Font face

Typeface headerFont = Typeface.createFromAsset(getAssets(), "raleway_extra_bold.otf");
public void setTypeface(TextView textView)
{
    if (textView != null)
    {
        if (textView.getTypeface() != null && textView.getTypeface().isBold())
        {
            textView.setTypeface(headerFont);
        }
    }
}

Customize as per requirement