- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 Google Plus 帐户登录我的应用程序。我编写了此代码,但显示强制关闭错误。
主要 Activity 代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
private static final int RC_SIGN_IN = 0;
// Logcat tag
private static final String TAG = "MainActivity";
// Google client to interact with Google API
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;
private SignInButton btnSignIn;
private Button btnSignOut, btnRevokeAccess;
private TextView txtName, txtEmail;
private LinearLayout llProfileLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSignIn = (SignInButton) findViewById(R.id.btn_sign_in);
btnSignOut = (Button) findViewById(R.id.btn_sign_out);
btnRevokeAccess = (Button) findViewById(R.id.btn_revoke_access);
txtName = (TextView) findViewById(R.id.txtName);
txtEmail = (TextView) findViewById(R.id.txtEmail);
llProfileLayout = (LinearLayout) findViewById(R.id.llProfile);
// Button click listeners
btnSignIn.setOnClickListener(this);
btnSignOut.setOnClickListener(this);
btnRevokeAccess.setOnClickListener(this);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
/**
* Method to resolve any signin errors
*/
private void resolveSignInError() {
if (mConnectionResult.hasResolution()) {
try {
mIntentInProgress = true;
mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
} catch (SendIntentException e) {
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
// Store the ConnectionResult for later usage
mConnectionResult = result;
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
@Override
protected void onActivityResult(int requestCode, int responseCode,
Intent intent) {
if (requestCode == RC_SIGN_IN) {
if (responseCode != RESULT_OK) {
mSignInClicked = false;
}
mIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
@Override
public void onConnected(Bundle arg0) {
mSignInClicked = false;
Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
// Get user's information
getProfileInformation();
// Update the UI after signin
updateUI(true);
}
/**
* Updating the UI, showing/hiding buttons and profile layout
*/
private void updateUI(boolean isSignedIn) {
if (isSignedIn) {
btnSignIn.setVisibility(View.GONE);
btnSignOut.setVisibility(View.VISIBLE);
btnRevokeAccess.setVisibility(View.VISIBLE);
llProfileLayout.setVisibility(View.VISIBLE);
} else {
btnSignIn.setVisibility(View.VISIBLE);
btnSignOut.setVisibility(View.GONE);
btnRevokeAccess.setVisibility(View.GONE);
llProfileLayout.setVisibility(View.GONE);
}
}
/**
* Fetching user's information name, email, profile pic
*/
private void getProfileInformation() {
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String personGooglePlusProfile = currentPerson.getUrl();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
Log.e(TAG, "Name: " + personName + ", plusProfile: "
+ personGooglePlusProfile + ", email: " + email);
txtName.setText(personName);
txtEmail.setText(email);
// by default the profile url gives 50x50 px image only
// we can replace the value with whatever dimension we want by
// replacing sz=X
} else {
Toast.makeText(getApplicationContext(),
"Person information is null", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onConnectionSuspended(int arg0) {
mGoogleApiClient.connect();
updateUI(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* Button on click listener
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_sign_in:
// Signin button clicked
signInWithGplus();
break;
case R.id.btn_sign_out:
// Signout button clicked
signOutFromGplus();
break;
case R.id.btn_revoke_access:
// Revoke access button clicked
revokeGplusAccess();
break;
}
}
/**
* Sign-in into google
*/
private void signInWithGplus() {
if (!mGoogleApiClient.isConnecting()) {
mSignInClicked = true;
resolveSignInError();
}
}
/**
* Sign-out from google
*/
private void signOutFromGplus() {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
updateUI(false);
}
}
/**
* Revoking access from google
*/
private void revokeGplusAccess() {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
Plus.AccountApi.revokeAccessAndDisconnect(mGoogleApiClient)
.setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status arg0) {
Log.e(TAG, "User access revoked!");
mGoogleApiClient.connect();
updateUI(false);
}
});
}
}
}
强制关闭错误:
05-21 11:08:13.007 32103-32103/com.tellfa.googlepluslogin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tellfa.googlepluslogin, PID: 32103
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.common.ConnectionResult.hasResolution()' on a null object reference
at com.tellfa.googlepluslogin.MainActivity.resolveSignInError(MainActivity.java:84)
at com.tellfa.googlepluslogin.MainActivity.signInWithGplus(MainActivity.java:234)
at com.tellfa.googlepluslogin.MainActivity.onClick(MainActivity.java:215)
at com.google.android.gms.common.SignInButton.onClick(Unknown Source)
at android.view.View.performClick(View.java:4764)
at android.view.View$PerformClick.run(View.java:19844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
如何解决这个问题?
我想使用 Google Plus 帐户登录我的应用程序。我编写了此代码,但显示强制关闭错误。
最佳答案
您的字段mConnectionResult
可以为 null,这会导致应用在尝试访问它时抛出 NullPointerException。
解决方案是更改 onConnectionFailed 的处理方式这样它总是在您访问变量之前为其分配一个值。
@Override
public void onConnectionFailed(ConnectionResult result) {
// Always assign result first
mConnectionResult = result;
if (!result.hasResolution()) {
GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this,
0).show();
return;
}
if (!mIntentInProgress) {
if (mSignInClicked) {
// The user has already clicked 'sign-in' so we attempt to
// resolve all
// errors until the user is signed in, or they cancel.
resolveSignInError();
}
}
}
关于java - 如何修复 Android 上 G+ 中的 boolean com.google.android.gms.common.ConnectionResult.hasResolution() Null 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37359863/
我在这里做 google + integartion。我正在使用以下代码,但我面临的错误是:onConnectionFailed: ConnectionResult.getErrorCode() =
[编辑] - 赏金 我正在尝试将我的 Android 应用程序与 Google Drive 连接以同步 MP3 音频文件。需要使用 GoogleDriveAndroidApi 和 GoogleApiC
我已经实现了检查播放服务是否照常可用的代码: mGooglePlayServicesAvailable = false; GoogleApiAvailability apiAvailability =
我尝试将 Google 云端硬盘集成到我的应用程序中,但收效甚微。使用 Quick Start 存储库中的代码,每次选择用户帐户登录时,我都会收到以下错误消息: I/DriveDemo: Google
当我尝试按照有关如何检查是否安装了 Google Play 服务的示例进行操作时,我收到以下错误:servicesConnected 方法内的行中出现“connectionResult cannot
我正在制作一个应用程序并将 Google Drive Android API 集成到其中。我有一个主要 Activity ,然后是一个 fragment ,该 fragment 打开导致谷歌驱动器。但
我正在开发将 Google Drive 集成到我的应用程序中的应用程序。以下是我的代码,我只是从示例代码中复制而来,但在连接 Google Drive 时出现异常。 异常:onConnectionFa
我想制作一个简单的可穿戴应用程序并通过数据层进行连接。手持模块(使用:S5)一切正常,但可穿戴设备(使用:Moto 360)总是抛出错误: onConnectionFailed: Connection
为什么 GoogleApiClient 的 blockingConnect 方法返回 ConnectionResult.CANCELED 而在同一调用期间我从 OnConnectionFailedLi
已实现 this sample在手机和平板电脑上一切都很完美!但是在 Android TV ADT-1 v5.0.2 build LRX22G 中,这些 Eloquent 消息出现了。 GmsCl
google play 服务适用于 Lollipop ,但当涉及到 kitkat 和更低版本时,即使设备上安装了 google-play-services,它也会给出 ConnectionResult
我想使用 Google Plus 帐户登录我的应用程序。我编写了此代码,但显示强制关闭错误。 主要 Activity 代码: public class MainActivity extends App
我曾尝试使用 google fit 开发应用程序。我关注 This关联 当我尝试运行该应用程序时,出现以下错误 10-27 11:55:19.966:I/Google Fit(6016):连接失败。原
我是一名优秀的程序员,十分优秀!