Android 在JSONObject的onResponse方法中的ArrayAdapter中放置一个字符串数组会给出;“没有合适的建造商”;错误

Android 在JSONObject的onResponse方法中的ArrayAdapter中放置一个字符串数组会给出;“没有合适的建造商”;错误,android,android-volley,android-arrayadapter,Android,Android Volley,Android Arrayadapter,我正在使用Volley库从MySQL数据库中获取一些数据,并将其放入一个id为“city\u spinner”的微调器中。我有一个名为main活动的活动,还有一个名为bookings的片段。微调器位于片段“bookings”中。“bookings.java”的代码如下所示: @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedI

我正在使用
Volley库
从MySQL数据库中获取一些数据,并将其放入一个id为“city\u spinner”的微调器中。我有一个名为main活动的
活动
,还有一个名为bookings的片段。微调器位于片段“bookings”中。“bookings.java”的代码如下所示:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final RelativeLayout ld = (RelativeLayout)getView().findViewById(R.id.loader);
    final RelativeLayout fb = (RelativeLayout)getView().findViewById(R.id.form_body);
    final Spinner city_sp = (Spinner)getView().findViewById(R.id.city_sp);
    requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, dataUrl, (String)null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try{
                JSONArray city = response.getJSONArray("city");
                //I'll eventually put data from JSON into spinner, but even with String array it doesn't work.
                String[] cities = new String[]{
                        "Select a City",
                        "City1",
                        "City2",
                        "City3"
                };
                //Gives me error in line below
                ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
                        this,R.layout.my_spinner, cities
                );
                spinnerArrayAdapter.setDropDownViewResource(R.layout.my_spinner);
                city_sp.setAdapter(spinnerArrayAdapter);

            } catch (JSONException e){
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    requestQueue.add(jsonObjectRequest);
}
@覆盖
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
final RelativeLayout ld=(RelativeLayout)getView().findviewbyd(R.id.loader);
final RelativeLayout fb=(RelativeLayout)getView().findViewById(R.id.form_body);
最终微调器city_sp=(微调器)getView().findViewById(R.id.city_sp);
requestQueue=Volley.newRequestQueue(getActivity().getApplicationContext());
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.POST,dataUrl,(String)null,新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray city=response.getJSONArray(“城市”);
//我最终会将JSON中的数据放入spinner,但即使使用字符串数组,它也不起作用。
字符串[]城市=新字符串[]{
“选择一个城市”,
“城市1”,
“城市2”,
“城市3”
};
//在下面的行中给我错误
ArrayAdapter SpinnerayAdapter=新的ArrayAdapter(
这个,R.layout.my_spinner,城市
);
spinnerrayadapter.setDropDownViewResource(R.layout.my_微调器);
城市设置适配器(spinnerrayadapter);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
}
});
add(jsonObjectRequest);
}
我得到的错误是:

Error:(151, 64) error: no suitable constructor found for ArrayAdapter(anonymous Listener< JSONObject>>,int,String[])
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; < anonymous Listener< JSONObject>> cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable
(argument mismatch; <anonymous Listener< JSONObject>> cannot be converted to Context)
constructor ArrayAdapter.ArrayAdapter(Context,int,List< String>) is not applicable
(argument mismatch; < anonymous Listener< JSONObject>> cannot be converted to Context)
错误:(151,64)错误:找不到适合ArrayAdapter的构造函数(匿名侦听器>,int,String[])
构造函数ArrayAdapter.ArrayAdapter(上下文,int,int)不适用
(参数不匹配;>无法转换为上下文)
构造函数ArrayAdapter.ArrayAdapter(上下文、int、字符串[])不适用
(参数不匹配;>无法转换为上下文)
构造函数ArrayAdapter.ArrayAdapter(上下文、int、列表<字符串>)不适用
(参数不匹配;>无法转换为上下文)

将该行更改为下方

ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
                        getActivity(),R.layout.my_spinner, cities
                );
ArrayAdapter微调器ArrayAdapter=新的ArrayAdapter(
getActivity(),R.layout.my_微调器,城市
);
JsonObjectRequest

的参数中,也将
(String)null
替换为
null
,很高兴我能帮忙:)快乐编码!