error: no suitable constructor found for ArrayAdapter(Tab1Fragment,int,ArrayList<String>)

error: no suitable constructor found for ArrayAdapter(Tab1Fragment,int,ArrayList<String>)

我是编程新手,一直在尝试将列表视图 + arrayadapter 添加到学校项目的片段中。已经提到这个问题但有用。

package com.tabian.tabfragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;    
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import android.widget.Button;


public class Tab1Fragment extends Fragment {
private static final String TAG = "Tab1Fragment";

private ListView listOfLeagues;
final ArrayList<String> listItems = new ArrayList<String>();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab1_fragment,container,false);
    listOfLeagues = (ListView) view.findViewById(R.id.listOfLeagues);

    String[] values = new String[] { "World Cup", "Champions League" };

    for(int i = 0; i < values.length; i++){
        listItems.add(values[i]);
    }

    ArrayAdapter adapter = new ArrayAdapter<(Tab1Fragment.this,
            android.R.layout.simple_list_item_1, listItems);
    listOfLeagues.setAdapter(adapter);

    listOfLeagues.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String selectedLeague = listItems.get(position).toString();

            Intent titleIntent = new Intent(view.getContext(), abc.class);

            titleIntent.putExtra("league", selectedLeague);
            startActivity(titleIntent);
        }
    });

    return view;
}

}

ArrayAdapter 构造函数的第一个参数应该是 Context 但你传递的是 FragmentTab1Fragment.this.

在生命周期处于附加状态的Fragment中,可以得到ContextgetContext()。因此,将 Tab1Fragment.this 替换为 getContext().