错误 - 没有找到适合 ArrayAdapter 的构造函数

Error - No suitable constructor found for ArrayAdapter

我在 android 应用程序的选项卡之一中有此代码:-

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        String str[]={"Arun","Mathev","Vishnu","Vishal","Arjun",
                "Arul","Balaji","Babu","Boopathy","Godwin","Nagaraj"};

        AutoCompleteTextView t1 = (AutoCompleteTextView)
                rootView.findViewById(R.id.autoCompleteTextView1);

        ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line,str);

        t1.setThreshold(1);
        t1.setAdapter(adp);
        return rootView;

并且以下行带有红色下划线,因此导致错误

(this,android.R.layout.simple_dropdown_item_1line,str)

我尝试编辑它,但问题仍然存在

这是您使用的 ArrayAdapter 的默认构造函数,您应该将 Context 作为第一个参数传递

ArrayAdapter (Context context, int resource, List objects)

所以改变

ArrayAdapter<String> adp=new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line,str);

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

尝试使用:

ArrayAdapter<String> adp=new ArrayAdapter<String>(
    container.getContext(),                
    android.R.layout.simple_dropdown_item_1line,
    str);

Fragment 本身不是 Context,您需要将上下文作为第一个参数传递:-)

作为旁注,您也可以使用 getActivity() 而不是 container.getContext(),但是如果未附加 Fragment 可能会产生异常(如果您使用Fragments 例如在适配器中)