Ila Icamsab Ila Icamsab - 9 months ago 45
Android Question

Unsure how to resolve "Cannot resolve constructor" error

I am getting an error on a particular fragment (which is one of three on a tabbed activity). The error is:

Cannot resolve constructor 'ArrayAdapter(layout.OneWayFragmen,int,java.lang.string[])'

I am trying to include and autocomplete field on one fragment of a tabbed activity. The java class in question is below:

package layout;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import com.example.alibasmaci.maral.R;

* A simple {@link Fragment} subclass.
public class OneWayFragment extends Fragment {

public OneWayFragment() {
// Required empty public constructor
public static OneWayFragment newInstance() {
OneWayFragment fragment = new OneWayFragment();
return fragment;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_one_way, container, false);

String[] cities =

{"Ottawa - Carp CYRP",
"Toronto - Billy Bishop CYYZ",
"Montreal - St. Hubert CYHU"

AutoCompleteTextView actvDeparture;
actvDeparture = (AutoCompleteTextView) rootView.findViewById(;
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.select_dialog_item,cities);

return rootView;


The error I am referring to is a result of this line:

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.select_dialog_item,cities);

Any thoughts on how to fix this?



ArrayAdapter's constructor takes the Context as the first parameter, and you're passing this from Fragment, which essentially means you're passing a Fragment in this line:

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.select_dialog_item,cities);

What you should do is pass the activity that it's attached to like:

ArrayAdapter adapter = new ArrayAdapter(getActivity(),android.R.layout.select_dialog_item,cities);

An even better way to do it is to declare it as a field and initialize it in onAttach method like:

ArrayAdapter adapter;

public void onAttach(Context context) {
    adapter = new ArrayAdapter(context,android.R.layout.select_dialog_item,cities);

EDIT: You're also missing this line: