pavol.franek
pavol.franek

Reputation: 1416

AutoCompleteTextView in fragment get NullPointerException

I try use AutoCompleteTextView in fragment but I get exception at autoCompleteTextViewCountry.setAdapter(adapter); Im use sample code but this sample was using it in standard activity not in fragment so I think problem is here:

My code: ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.iMainActivity, android.R.layout.simple_dropdown_item_1line, COUNTRIES);

Original sample code: ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, COUNTRIES);

Full class:

    public class SettingsActivity extends Activity {
   public static SettingsActivity iSettingsActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }
    }

    public SettingsActivity(){
     iSettingsActivity=this;
    }

    public static class PlaceholderFragment extends Fragment {
        public PlaceholderFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_settings, container, false);

            String COUNTRIES[]={"INDIA","ITALY","JAPAN","USA","ICELAND","INDONESIA","UK","IRAN","IRAQ"};
            final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView1);
            final TextView textViewSelectedCountry=(TextView)rootView.findViewById(R.id.textViewCountry);
            Button btnSelectedCountry=(Button)rootView.findViewById(R.id.buttonSelectCountry);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.iMainActivity,
                    android.R.layout.simple_dropdown_item_1line, COUNTRIES);

            autoCompleteTextViewCountry.setAdapter(adapter);
            autoCompleteTextViewCountry.setThreshold(1);

            btnSelectedCountry.setOnClickListener(new View.OnClickListener() {
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    String country=autoCompleteTextViewCountry.getText().toString();
                    textViewSelectedCountry.setText("Selected Country: "+country);
                }
            });
            return rootView;
        }
    }
}

Full xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".SettingsActivity$PlaceholderFragment"
android:background="@color/myBlack"
    >

<TextView
    android:id="@+id/textViewCountry"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="You Selected :"
    android:textColor="@color/white"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:layout_below="@+id/autoCompleteTextView"
    android:layout_alignParentLeft="true" />

<AutoCompleteTextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:layout_below="@+id/autoCompleteTextView1"
    android:layout_marginTop="20dp"
    android:layout_alignParentLeft="true"
    android:ems="10"
    android:hint="Choose The Country"
    android:id="@+id/autoCompleteTextView">
    <requestFocus />
</AutoCompleteTextView>

<Button
    android:id="@+id/buttonSelectCountry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="          OK          "`enter code here`
    android:layout_below="@+id/textViewCountry"
    android:layout_alignParentLeft="true" />

</RelativeLayout>

Upvotes: 1

Views: 3041

Answers (4)

Mayank Bhatnagar
Mayank Bhatnagar

Reputation: 2191

The AutoComplete Textview you are inflating using wrong id autoCompleteTextView1.

Try this:

final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView);

instead of

final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView1);

Also change the below statement

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.iMainActivity,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);

to

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);

I hope this works for you.

Upvotes: 0

Apoorv
Apoorv

Reputation: 13520

Change this line

final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView1);

to

final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)rootView.findViewById(R.id.autoCompleteTextView);

You are passing wrong id hence it is giving NullPointerException.

Upvotes: 3

Israel Tabadi
Israel Tabadi

Reputation: 574

in this Line : ArrayAdapter adapter = new ArrayAdapter(MainActivity.iMainActivity, android.R.layout.simple_dropdown_item_1line, COUNTRIES);

the first parameter the ArrayAdapter should be the context, when fragment is hosted in an Activity, in this case (MainActivity) the context should be - getActivity()

the fragment shouldn't have a specific knowledge of it's parent hosting Activity.

change it, run it and it should work :)

Upvotes: 1

kalyan pvs
kalyan pvs

Reputation: 14590

Change this line

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.iMainActivity,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);

to like this..

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);

And remove the static field..

Upvotes: 2

Related Questions