本文介绍了无法解析构造函数ArrayAdapter(android.Content.Context,int,java.util.ArrayList< MyObject>)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know this question has been asked a million times before, but I have tried all of the solutions I can find, and still doesn't work. I have tried calling "this" for context, I have tried getActivity, I have tried getContext(), but nothing seems to work for this fragment in particular. The same code does work in a different fragment tho, which is why I am really confused. Any help appreciated.

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

My LoginFragment, my issue can be found in 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);
    }
}

代码格式

推荐答案

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

You probably should learn more about generics and type safety.

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

Your reservations has type ArrayList<Reservation> but you try to create ArrayAdapter<Review> which is wrong. Change it to ArrayAdapter<Reservation>.

这篇关于无法解析构造函数ArrayAdapter(android.Content.Context,int,java.util.ArrayList&lt; MyObject&gt;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:35