簡體   English   中英

無法在android中集成g + login

[英]Unable to integrate g+ login in android

我一直在嘗試為我的應用創建一個登錄頁面,並決定使用G +作為登錄方式之一。

我的代碼如下

public class LoginActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
String Name,Pass;
private static final int RC_SIGN_IN = 0;
private static final String TAG = "MainActivity";
private GoogleApiClient mGoogleApiClient;
private boolean mIntentInProgress;
private boolean mSignInClicked;
private ConnectionResult mConnectionResult;
SharedPreferences preferences;
SharedPreferences.Editor edit;
SignInButton b3;
final String App_id="WP74ib9AoAQtnKbJxgz5FkMSQLDq1raIIVyqIqup",C_key="FUXJl7JcyGP7cJuktrdoBr8WtnpzjpIQ9UK1UWc6";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    b3=(SignInButton)findViewById(R.id.button3);
    mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Plus.API)
            .addConnectionCallbacks(this).addOnConnectionFailedListener(this)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();
    b3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            click();
        }
    });
}
public void click(){
    signInWithGplus();
    Toast.makeText(getApplicationContext(),"Inside click",Toast.LENGTH_SHORT).show();
}
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

protected void onStop() {
    super.onStop();
    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}
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) {
            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;
    getProfileInformation();
}

@Override
public void onConnectionSuspended(int arg0) {
    mGoogleApiClient.connect();

}
private void signInWithGplus() {
    if (!mGoogleApiClient.isConnecting()) {
        mSignInClicked = true;
        resolveSignInError();
    }
    else
        getProfileInformation();
}

/**
 * Method to resolve any signin errors
 * */
private void resolveSignInError() {
    if (mConnectionResult.hasResolution()) {
        try {
            mIntentInProgress = true;
            mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
        } catch (IntentSender.SendIntentException e) {
            Toast.makeText(getApplicationContext(),
                    "SignIn Error", Toast.LENGTH_LONG).show();
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }
}

/**
 * Fetching user's information name, email, profile pic
 * */
private void getProfileInformation() {
    try {
        //Toast.makeText(getApplicationContext(), "Getting Profile",Toast.LENGTH_SHORT).show();
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
            Person currentPerson = Plus.PeopleApi
                    .getCurrentPerson(mGoogleApiClient);
            String personName = currentPerson.getDisplayName();
            String personPhotoUrl = currentPerson.getImage().getUrl();
            String personGooglePlusProfile = currentPerson.getUrl();
            String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
            Toast.makeText(getApplicationContext(),"Got Information",Toast.LENGTH_SHORT).show();
            Log.e(TAG, "Name: " + personName + ", plusProfile: "
                    + personGooglePlusProfile + ", email: " + email
                    + ", Image: " + personPhotoUrl);
        } else {
            //Toast.makeText(getApplicationContext(),
            //        "Person information is null", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        // Toast.makeText(getApplicationContext(),
        //         "Person information is null", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    //if (id == R.id.next) {
     //   return true;
    //}

    return super.onOptionsItemSelected(item);
}    }

我已經實現了獲取數據所需的所有必需功能。

當我啟動應用程序並單擊登錄按鈕時,我得到一個空指針異常

if (mConnectionResult.hasResolution())

錯誤日志如下所示

java.lang.NullPointerException
        at com.example.nirmal.playreskey.LoginActivity.resolveSignInError(LoginActivity.java:161)
        at com.example.nirmal.playreskey.LoginActivity.signInWithGplus(LoginActivity.java:151)
        at com.example.nirmal.playreskey.LoginActivity.click(LoginActivity.java:92)
        at com.example.nirmal.playreskey.LoginActivity$3.onClick(LoginActivity.java:87)
        at com.google.android.gms.common.SignInButton.onClick(Unknown Source)
        at android.view.View.performClick(View.java:4463)
        at android.view.View$PerformClick.run(View.java:18770)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
        at dalvik.system.NativeStart.main(Native Method)

我搜索了這種類型的錯誤,發現如果我們在執行onStart()之前嘗試調用signin函數,就會發生這種情況。 所以我創建了一個toast來檢查onStart()是否被執行然后單擊按鈕然后我也得到了同樣的錯誤。

這個錯誤的原因是什么? 以及如何糾正它?

我按照androidhive的教程。 這幾乎是同一個教程

private void resolveSignInError() {
        if (mConnectionResult != null) { //Added null check 
            if (mConnectionResult.hasResolution()) {
                try {
                    mIntentInProgress = true;
                    mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
                } catch (IntentSender.SendIntentException e) {
                    mIntentInProgress = false;
                    mGoogleApiClient.connect();
                }
            }
        }
    }

您可以使用它來處理該方法中的null異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM