在片段 java class 中使用 "this" 和 getLastSignedInAccount 时出现错误
In fragment java class getting an error when using "this" with getLastSignedInAccount
在我的片段 java class 中,“this”语句中的 getLastSignedInAccount 出现错误,代码如下:
GoogleSignedInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this);
if(signInAccount != null){
name.setText(signInAccount.getDisplayName());
mail.setText(signInAccount.getEmail());
}
在片段中你需要使用 getContext() 而不是这个
片段是可重复使用的 class 实现 activity 的一部分。 Fragment 通常定义用户界面的一部分。片段必须嵌入到活动中;他们不能 运行 独立于活动。所以我的意思是你不能从嵌入式片段中获得 Access Main Activity,所以有一个像这样的代码而不是 (this) :
GoogleSignIn.getLastSignedInAccount(getActivity());
片段不子class上下文class。因此,您必须使用 getActivity() 方法来获取父级 activity.
在我的片段 java class 中,“this”语句中的 getLastSignedInAccount 出现错误,代码如下:
GoogleSignedInAccount signInAccount = GoogleSignIn.getLastSignedInAccount(this);
if(signInAccount != null){
name.setText(signInAccount.getDisplayName());
mail.setText(signInAccount.getEmail());
}
在片段中你需要使用 getContext() 而不是这个
片段是可重复使用的 class 实现 activity 的一部分。 Fragment 通常定义用户界面的一部分。片段必须嵌入到活动中;他们不能 运行 独立于活动。所以我的意思是你不能从嵌入式片段中获得 Access Main Activity,所以有一个像这样的代码而不是 (this) :
GoogleSignIn.getLastSignedInAccount(getActivity());
片段不子class上下文class。因此,您必须使用 getActivity() 方法来获取父级 activity.