-2

In my fragment java class there is an error coming in the getLastSignedInAccount in "this" statement following is the code:

GoogleSignedInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this); 
 if(signInAccount != null){
    name.setText(signInAccount.getDisplayName()); 
    mail.setText(signInAccount.getEmail());
   }
Shubham Jain
  • 2,365
  • 2
  • 17
  • 29
  • 1
    Welcome to stackoverflow. please go through this link once. https://stackoverflow.com/help/how-to-ask – WhiteSpidy. Aug 24 '20 at 05:14
  • Method defined as `#getLastSignedInAccount(Context context)` . You have pass context . In fragment you can use `getContext()` to use context. – ADM Aug 24 '20 at 05:42

2 Answers2

0

In fragment you need use getContext() instead of this

  • even if your answer is correct, it doesn't teach OP (or anyone) _why_ they have to change it, consider adding that to your answer – a_local_nobody Aug 24 '20 at 05:45
0

A fragment is a reusable class implementing a portion of an activity. A Fragment typically defines a part of a user interface. Fragments must be embedded in activities; they cannot run independently of activities. so what i mean is you cannot get Access Main Activity from Embedded Fragment, so have a code something Like this instead of (this) :

GoogleSignIn.getLastSignedInAccount(getActivity());

Fragments don’t subclass the Context class. Therefore you have to use the getActivity() method to get the parent activity.

Ganesh
  • 169
  • 6