在片段中集成google plus api时出现ClassCastException

时间:2014-09-10 11:51:27

标签: android android-fragments google-plus

我按照developer.google.com中提到的所有步骤在我的应用程序中实现了googleplus api。

我正在片段中集成googleplus api,当我运行代码时,我得到了ClassCastException。 我的片段类:

 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();
        }
    }
}

在此行获取错误:

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

Error implementing GoogleApiClient Builder for Android development中给出的解决方案没有为我锻炼......

1 个答案:

答案 0 :(得分:2)

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

那是错误的,该方法接受实现GoogleApiClient.ConnectionCallbacks的类,使用Fragment代替getActivity().getApplicationContext()