java 不确定如何解决“无法解析构造函数”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39025076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:01:37  来源:igfitidea点击:

Unsure how to resolve "Cannot resolve constructor" error

javaandroidandroid-studio

提问by Ila Icamsab

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:

我正在尝试在选项卡式活动的一个片段上包含和自动完成字段。有问题的java类如下:

package layout;
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;
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;
    }

    @Override
    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(R.id.actvDeparture);
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.select_dialog_item,cities);
        actvDeparture.setThreshold(1);

        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?

关于如何解决这个问题的任何想法?

Thanks

谢谢

采纳答案by Vucko

ArrayAdapter's constructor takes the Contextas the first parameter, and you're passing thisfrom Fragment, which essentially means you're passing a Fragmentin this line:

ArrayAdapter 的构造函数将 theContext作为第一个参数,并且您正在传递thisfrom Fragment,这实际上意味着您Fragment在此行中传递 a :

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 onAttachmethod like:

一个更好的方法是将它声明为一个字段并在如下onAttach方法中初始化它:

ArrayAdapter adapter;

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

EDIT: You're also missing this line:

编辑:您还缺少这一行:

actvDeparture.setAdapter(adapter);

回答by Kakey

ArrayAdapter accepts Context for the first parameter and you can pass the context by calling getActivity().

ArrayAdapter 接受 Context 作为第一个参数,您可以通过调用 getActivity() 传递上下文。

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

回答by ΦXoc? ? Пepeúpa ツ

The error is that there is no such constructor like:

错误是没有这样的构造函数,例如:

 ArrayAdapter(Fragment, int, T[])

Change the reference thisfor getActivity()so you can get the context propely,

更改参考thisgetActivity(),所以你可以propely获取上下文,

after that it will match the constructor

之后它将匹配构造函数

ArrayAdapter(Context context, int resource, T[] objects)

回答by Andreas

ArrayAdapteris a genericclass. You haven't specified the generic type argument, so you're using it raw, which is bad. It should be ArrayAdapter<String>.

ArrayAdapter是一个泛型类。您尚未指定泛型类型参数,因此您使用的是raw,这很糟糕。应该是ArrayAdapter<String>

It has 6 constructors:

它有6个构造函数:

ArrayAdapter(Context context, int resource)
ArrayAdapter(Context context, int resource, int textViewResourceId)
ArrayAdapter(Context context, int resource, T[] objects)
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
ArrayAdapter(Context context, int resource, List<T> objects)
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

You method call has this signature:

您的方法调用具有以下签名:

ArrayAdapter(layout.OneWayFragmen,int,java.lang.string[])

That mightfit the 3rd constructor, but only if layout.OneWayFragmenis a Context. Is it?

可能适合第三个构造函数,但前提layout.OneWayFragmenContext. 是吗?