Vinay Joshi
Vinay Joshi

Reputation: 97

Plusclient.connect() in android application is not working

I want to integrate google+ sign in my android app. I have given permissions in android manifest file and also initialized plusclient object but when using sign in button I'm getting an error "Unfortunately application has stopped!".

Android manifest file is:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.abs"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="android.permission.USE_CREDENTIALS"/>


    <application
        android:allowBackup="true"
        android:icon="@drawable/abs_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.abs.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.abs.Bank"
            android:label="@string/title_activity_bank" >
        </activity>
        <activity
            android:name="com.abs.Scheme"
            android:label="@string/title_activity_scheme" >
        </activity>
        <activity
            android:name="com.abs.Login"
            android:label="@string/title_activity_login" >

        </activity>
    </application>

</manifest>

Mainactivity is:

package com.abs;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {


    Button bank;
    Button scheme;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


     bank = (Button) findViewById(R.id.button1);
     scheme = (Button) findViewById(R.id.button2);


    }



    public void onClick_bank(View v){
        Toast.makeText(MainActivity.this, "Searching by bank", Toast.LENGTH_SHORT).show();

        Intent ibank = new Intent(v.getContext(),Bank.class);
        startActivityForResult(ibank, 0);

    }

    public void onClick_scheme(View v){
        Toast.makeText(MainActivity.this, "Searching by scheme", Toast.LENGTH_SHORT).show();

        Intent ischeme = new Intent(v.getContext(),Scheme.class);
        startActivityForResult(ischeme, 0);
    }

    public void onClick_login(View v){

        Toast.makeText(MainActivity.this, "Please login with gmail id", Toast.LENGTH_SHORT).show();
        Intent islog = new Intent(v.getContext(),Login.class);
        startActivityForResult(islog, 0);
    }

    @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;
    }




}

Login activity for google+ sign in is given below and i have taken the code from google examples: for switching to login.class from mainactivity i had to comment onstart() because mplusclient.connect() is not working, Please help...

package com.abs;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.plus.PlusClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class Login extends Activity implements OnClickListener,
        PlusClient.ConnectionCallbacks, PlusClient.OnConnectionFailedListener,
        PlusClient.OnAccessRevokedListener {

    private static final int DIALOG_GET_GOOGLE_PLAY_SERVICES = 1;

    private static final int REQUEST_CODE_SIGN_IN = 1;
    private static final int REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES = 2;

    private TextView mSignInStatus;
    private PlusClient mPlusClient;
    private SignInButton mSignInButton;
    private View mSignOutButton;
    private View mRevokeAccessButton;
    private ConnectionResult mConnectionResult;
    private ProgressDialog mConnectionProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mPlusClient = new PlusClient.Builder(Login.this, Login.this, Login.this)
                .setActions("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity")
                .setScopes(Scopes.PLUS_LOGIN)
                .build();
        mConnectionProgressDialog = new ProgressDialog(this);
        mConnectionProgressDialog.setMessage("Signing in...");

        mSignInStatus = (TextView) findViewById(R.id.sign_in_status);
        mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
        mSignInButton.setOnClickListener(this);
        mSignOutButton = findViewById(R.id.sign_out_button);
        mSignOutButton.setOnClickListener(this);
        mRevokeAccessButton = findViewById(R.id.revoke_access_button);
        mRevokeAccessButton.setOnClickListener(this);
    }
    /*
    @Override
    public void onStart() {
        super.onStart();
        mPlusClient.connect();
    }

    @Override
    public void onStop() {
        mPlusClient.disconnect();
        super.onStop();
    }
*/
    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View view) {

        switch(view.getId()) {
        case R.id.sign_in_button:
            if(!mPlusClient.isConnected()){

                mPlusClient.connect();
            }
            //mPlusClient.connect();

            /*  
            int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(Login.this);
                    if (available == ConnectionResult.SUCCESS) {
                        showDialog(DIALOG_GET_GOOGLE_PLAY_SERVICES);
                     return;
                 }

                 try {
                     mSignInStatus.setText("Signing in");
                     mConnectionResult.startResolutionForResult(Login.this, REQUEST_CODE_SIGN_IN);
                 } 
                 catch (IntentSender.SendIntentException e) {
                     // Fetch a new result to start.
                     mPlusClient.connect();
                 }
            */
            break;
        case R.id.sign_out_button:

            if (mPlusClient.isConnected()) {
                mPlusClient.clearDefaultAccount();
                mPlusClient.disconnect();
                mPlusClient.connect();
            }
            else{Toast.makeText(Login.this, "You are not connected to internet", Toast.LENGTH_LONG).show();}
            break;
        case R.id.revoke_access_button:

            if (mPlusClient.isConnected()) {
                mPlusClient.revokeAccessAndDisconnect(this);
                updateButtons(false /* isSignedIn */);
            }
            else{Toast.makeText(Login.this, "You are not connected to internet", Toast.LENGTH_LONG).show();}
            break;
    }

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        if (id != DIALOG_GET_GOOGLE_PLAY_SERVICES) {
            return super.onCreateDialog(id);
        }

        int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (available == ConnectionResult.SUCCESS) {
            return null;
        }
        if (GooglePlayServicesUtil.isUserRecoverableError(available)) {
            return GooglePlayServicesUtil.getErrorDialog(
                    available, Login.this, REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES);
        }
        return new AlertDialog.Builder(this)
                .setMessage("+ generic error")
                .setCancelable(true)
                .create();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(Login.this, "activity result disabled", Toast.LENGTH_LONG).show();
         if (requestCode == REQUEST_CODE_SIGN_IN
                || requestCode == REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES) {
            if (resultCode == RESULT_OK && !mPlusClient.isConnected()
                    && !mPlusClient.isConnecting()) {
                // This time, connect should succeed.
                mPlusClient.connect();
            }
        }
    }

    @Override
    public void onAccessRevoked(ConnectionResult status) {
        Toast.makeText(Login.this, "Access revoke not available", Toast.LENGTH_LONG).show();
         if (status.isSuccess()) {
            mSignInStatus.setText("revoke access status");
        } else {
            mSignInStatus.setText("revoke access status error");
            mPlusClient.disconnect();
        }
        mPlusClient.connect();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        Toast.makeText(Login.this, "Client is connected", Toast.LENGTH_LONG).show();
          String currentPersonName = mPlusClient.getCurrentPerson() != null
                ? mPlusClient.getCurrentPerson().getDisplayName()
                : "Signed in currentPersonName";
        mSignInStatus.setText("signed in as you");
        updateButtons(true );/* isSignedIn */
    }

    @Override
    public void onDisconnected() {
        Toast.makeText(Login.this, "Client is disconnected", Toast.LENGTH_LONG).show();
          mSignInStatus.setText("Loading status");
        mPlusClient.connect();
        updateButtons(false );/* isSignedIn */
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Toast.makeText(Login.this, "Connection failed", Toast.LENGTH_LONG).show();
          mConnectionResult = result;
        updateButtons(false );/* isSignedIn */
    }

    private void updateButtons(boolean isSignedIn) {
        if (isSignedIn) {
            mSignInButton.setVisibility(View.INVISIBLE);
            mSignOutButton.setEnabled(true);
            mRevokeAccessButton.setEnabled(true);
        } else {
            if (mConnectionResult == null) {
                // Disable the sign-in button until onConnectionFailed is called with result.
                mSignInButton.setVisibility(View.INVISIBLE);
                mSignInStatus.setText("Loading status");
            } else {
                // Enable the sign-in button since a connection result is available.
                mSignInButton.setVisibility(View.VISIBLE);
                mSignInStatus.setText("Signed out");
            }

            mSignOutButton.setEnabled(false);
            mRevokeAccessButton.setEnabled(false);
        }
    }
}

Thank you!!

Upvotes: 0

Views: 887

Answers (1)

iFunDroid
iFunDroid

Reputation: 118

I have been messing with the same code and finally got it running with some modifications.

According to me mPlusClient.connect(); must be called before click as it connects async and if used in the onclick it will give error.

here is my onclick() function

@Override
public void onClick(View arg0) {

    int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (available != ConnectionResult.SUCCESS) {
        showGoogleDialog();
        return;
    }
    try {
        mConnectionResult.startResolutionForResult(this, REQUEST_CODE_SIGN_IN);
    } catch (IntentSender.SendIntentException e) {
        mPlusClient.connect();
    } catch (NullPointerException e1) {
        PvrLog.d("null pointer exception");
    }
}

Upvotes: 1

Related Questions