1

I followed all steps mentioned in developer.google.com to implement the googleplus api in my application.

I'm integrating the googleplus api in the fragment and i'm getting an ClassCastException when i run the code. My Fragment Class:

 import com.digiapes.apeonomy.animation.Positions;
    import com.example.apeonomy.R;
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GooglePlayServicesUtil;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
    import com.google.android.gms.plus.Plus;
   import android.animation.ObjectAnimator;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;


    public class Login extends Fragment implements OnGlobalLayoutListener,
            OnClickListener, GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener {
        EditText UN, Pass;
        View loginimage, loginusing, loginlayout;
        ImageButton flogin, Glogin;
        Button login1;
        float f;
        Positions pos = new Positions();
        String name = "Harsha";
        String pass = "hahaha";
        private static final int RC_SIGNIN = 0;
        private static boolean mSignInClicked;
        private GoogleApiClient mGoogleApiClient;
        private boolean mIntentInProgress;
        private boolean mSignedIn;
        private ConnectionResult mConnectionResult;
        private Activity c;
        private boolean mSignedInClicked;

        // Login_Register Lg=new Login_Register();
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View login = inflater.inflate(R.layout.login, container, false);
            // TODO Auto-generated method stub
            UN = (EditText) login.findViewById(R.id.UsernameL);
            Pass = (EditText) login.findViewById(R.id.PassswordL);
            loginimage = login.findViewById(R.id.LoginImage);
            login1 = (Button) login.findViewById(R.id.Login);
            loginusing = login.findViewById(R.id.loginusing);
            loginlayout = login.findViewById(R.id.LoginLayout);
            f = ((Login_Register) getActivity()).pos.getLIpos();
            loginimage.getViewTreeObserver().addOnGlobalLayoutListener(this);
            UN.getBackground().setAlpha(50);
            Pass.getBackground().setAlpha(50);
            ObjectAnimator.ofFloat(loginusing, View.ALPHA, 0, 1).setDuration(1000)
                    .start();
            ObjectAnimator.ofFloat(loginlayout, View.ALPHA, 0, 1).setDuration(1000)
                    .start();
            login.findViewById(R.id.googlepluslog).setOnClickListener(this);
            login.findViewById(R.id.facebooklog).setOnClickListener(this);

            return login;
        }

        @Override
        public void onGlobalLayout() {
            pos.setLIpos(loginimage.getY());
            // TODO Auto-generated method stub

        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onActivityCreated(savedInstanceState);
            ObjectAnimator
                    .ofFloat(loginimage, View.TRANSLATION_Y,
                            f - loginimage.getY() - 130, 0).setDuration(300)
                    .start();
            login1.setOnClickListener(this);

        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks((ConnectionCallbacks) getActivity().getApplicationContext())
            .addOnConnectionFailedListener(this).addApi(Plus.API, null)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

        }

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.Login:
                if (UN.getText().length() == 0 || Pass.getText().length() == 0) {
                    Toast.makeText(getActivity(),
                            "Please enter valid Username and Password",
                            Toast.LENGTH_SHORT).show();
                } else if (UN.getText().toString().equals(name)
                        && Pass.getText().toString().equals(pass)) {

                    Intent intent = new Intent(getActivity()
                            .getApplicationContext(), Main.class);
                    startActivity(intent);
                }
            break;
        case R.id.facebooklog:

            break;
        case R.id.googlepluslog:
            signInWithGplus();
            break;

        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (requestCode == RC_SIGNIN) {
            mIntentInProgress = false;

            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect();
            }
        }
    }
    private void signInWithGplus() {
        // TODO Auto-generated method stub
        if (!mGoogleApiClient.isConnecting()) {
            mSignedIn = true;
            resolveSignInerror();
        }

    }
    private void resolveSignInerror() {
        // TODO Auto-generated method stub
        if (mConnectionResult.hasResolution()) {
            try {
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(c, RC_SIGNIN);
            } catch (SendIntentException e) {
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // TODO Auto-generated method stub
        if (!result.hasResolution()) {
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), c, 0)
                    .show();
            return;
        }
        if(!mIntentInProgress){
            mConnectionResult=result;
            if(mSignedInClicked)
                resolveSignInerror();
        }
    }

    @Override
    public void onConnected(Bundle arg0) {
        mSignedInClicked=false;
        Toast.makeText(c, "Connected", Toast.LENGTH_SHORT).show();
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectionSuspended(int arg0) {
        // TODO Auto-generated method stub
        mGoogleApiClient.connect();

    }
    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        mGoogleApiClient.connect();
    }
    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }
}

Getting error at this line:

mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks((ConnectionCallbacks) getActivity().getApplicationContext())
            .addOnConnectionFailedListener(this).addApi(Plus.API, null)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

Logcat:enter image description here

Solution given in Error implementing GoogleApiClient Builder for Android development didn't workout for me...

Community
  • 1
  • 1
Harsha
  • 659
  • 10
  • 23
  • change to mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this).addApi(Plus.API, null) .addScope(Plus.SCOPE_PLUS_LOGIN).build(); – Ravi Kant Sep 10 '14 at 11:57
  • that didn't workout.. – Harsha Sep 10 '14 at 11:57

1 Answers1

2
.addConnectionCallbacks((ConnectionCallbacks) getActivity().getApplicationContext())

That's wrong, the method accepts a class that implements GoogleApiClient.ConnectionCallbacks, use your Fragment instead of getActivity().getApplicationContext().

2Dee
  • 8,609
  • 7
  • 42
  • 53
  • yes i did what you mentioned and it worked out, and also i had to remove null in .addApi(Plus.API, null).. – Harsha Sep 10 '14 at 12:06
  • Now i get a dialog to add choose/add an account and but my application shutdowns. – Harsha Sep 10 '14 at 12:08
  • My advice would be to try to investigate that, and create a new question if you cannot find a solution. Your new error might be related to something else entirely, since you're getting the login dialog... – 2Dee Sep 10 '14 at 12:10
  • Glad I could help and best of luck for the rest of your app development ;) – 2Dee Sep 10 '14 at 12:13