Cant make a new newRequestQueue In Volley, android

2019-06-20 04:09发布

I have an fragment Where I try to Instantiate a new newRequestQueue with the Volley API.

I try to Instantiate It like this:

RequestQueue queue = Volley.newRequestQueue(this);

However, When I try to create the request, I get the following error:

newRequestQueue In Volley cannot be applied to annonymous android.view.View.OnClickListener

Here Is my Fragment class:

public class LoginFragment extends Fragment {
    private FacebookLogin fLogin;
    private Profile profile;
    private CallbackManager mCallbackManager; //Used in activity result below. Gets a value when u hit the button
    private static final String TAG = "Event";
    private static String url_create_user = "127.0.0.1/create_row.php";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fLogin = new FacebookLogin(getActivity().getApplicationContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v =  inflater.inflate(R.layout.login, container, false);
        return v;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);

        final LoginButton loginButton = (LoginButton) getView().findViewById(R.id.login_button);
        final TextView infoText = (TextView) getView().findViewById(R.id.text_details);
        final ImageView profilP = (ImageView) getView().findViewById(R.id.profilePicture);

        loginButton.setFragment(this);


        loginButton.setReadPermissions("user_friends");

        loginButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.i(TAG, " Button clickde");
                fLogin.setCallback(loginButton); //Let's register a callback

                RequestQueue queue = Volley.newRequestQueue(this);

                fLogin.setFacebookListener(new FacebookLogin.OnFacebookListener() {
                    @Override
                    public void onFacebookLoggedIn(JSONObject parameters) {
                        Log.i(TAG, "INNNNNNNNNNNE");
                        Log.i(TAG, parameters.toString());
                    }
                });
            }
        });

    }

   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mCallbackManager = fLogin.getCallbackManager(); //Get the callbackmanager
        mCallbackManager.onActivityResult(requestCode, resultCode, data);
    }

}

Anyone who can help me?

2条回答
贼婆χ
2楼-- · 2019-06-20 04:30

the parameter of newRequestQueue is a Context object. In your case, this, refers to the View.OnClickListener anonymous inner class, where you are calling newRequestQueue. Change

Volley.newRequestQueue(this);

with

 Volley.newRequestQueue(getActivity().getApplicationContext());

is, of course, getActivity() because you are subclassing Fragment

查看更多
够拽才男人
3楼-- · 2019-06-20 04:42
RequestQueue requestQueue = Volley.newRequestQueue(SignupActivity.this);

In this case, SignupActivity is the activity you have typed your code in.

查看更多
登录 后发表回答