无法解析构造函数 ArrayAdapter(android.Content.Context, int, java.util.ArrayList<MyObject>)

Cannot resolve constructor ArrayAdapter(android.Content.Context, int, java.util.ArrayList<MyObject>)

我知道这个问题已经被问过一百万次了,但我已经尝试了所有我能找到的解决方案,但仍然没有用。我试过为上下文调用 "this",我试过 getActivity,我试过 getContext(),但似乎对这个片段没有任何作用。相同的代码确实在不同的片段中工作,这就是为什么我真的很困惑。任何帮助表示赞赏。

我的LoginFragment,我的问题可以在setReservations()中找到:

public class LoginFragment extends Fragment {
    LoginButton loginButton;
    TextView nameText;
    ProfileTracker mProfileTracker;
    DBHandler dbHandler;
    Context context;
    ArrayList<Reservation> reservations;
    ListView lv;
    Profile fbProfile;

    CallbackManager callbackManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.login, container, false);
        callbackManager = CallbackManager.Factory.create();
        dbHandler = new DBHandler(getContext(), null, null, 1);
        context = getActivity();
        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        nameText = (TextView) view.findViewById(R.id.users_name);
        loginButton.setReadPermissions("email");
        setmProfileTracker();
        mProfileTracker.startTracking();
        // If using in a fragment
        loginButton.setFragment(this);
        // Other app specific specialization

        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

            }

            @Override
            public void onCancel() {
                // App code
            }

            @Override
            public void onError(FacebookException exception) {
                // App code
            }
        });

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        fbProfile = Profile.getCurrentProfile();
        if (fbProfile != null)
        {
            updateUI();
        }
        getActivity().setTitle("My page");
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode,resultCode,data);
    }

    private void setmProfileTracker()
    {
        mProfileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile profile, Profile profile2) {
                updateUI(); //this is the third piece of code I will discuss below
            }
        };
    }

    private void updateUI() {

        boolean enableButtons = AccessToken.getCurrentAccessToken() != null;
        if (fbProfile == null) {
            fbProfile = Profile.getCurrentProfile();
            Log.e("Profile", "null");
        }
        if (enableButtons && fbProfile != null) {
            Log.e("Access Token", AccessToken.getCurrentAccessToken().toString());
            nameText.setText(fbProfile.getName());
        }
    }

    private void setReservations()
    {
        reservations = dbHandler.userReservationToArrayList(parseLong(fbProfile.getId()));

        lv = (ListView) getView().findViewById(R.id.reservations);

        ArrayAdapter<Review> arrayAdapter = new ArrayAdapter<Review>(
            context,
            android.R.layout.simple_list_item_1,
            reservations);

        lv.setAdapter(arrayAdapter);
    }
}

编辑:代码格式

您可能应该了解有关泛型和类型安全的更多信息。

您的 reservations 具有类型 ArrayList<Reservation> 但您尝试创建 ArrayAdapter<Review> 这是错误的。将其更改为 ArrayAdapter<Reservation>.

您的阵列适配器属于审查类型

ArrayAdapter<Review> arrayAdapter 

但是你的路过:

 ArrayList<Reservation> reservations
Change it to ArrayAdapter<Reservation>

您应该传递 Review 的数组而不是 Reservation 的数组。因此,将您的适配器更改为 ArrayAdapter<Reservation>.

此外,目前您在 onCreateView() 方法中调用 getActivity() 方法,您的 Activity 尚未创建,这可能会导致错误。 要解决此问题,您应该将此行移动到您的 onActivityCreated() :

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity();
    }