创建Google登录时出错

时间:2016-05-07 18:38:49

标签: java android

我正在尝试实施以下谷歌登录,但我收到以下错误:

public class login extends AppCompatActivity implements
        GoogleApiClient.OnConnectionFailedListener,
        View.OnClickListener  {

    private LoginButton loginButton;
    private CallbackManager callbackManager;
    private Button mButton;
    private TextView mTextDetail;
    private TextView mTextAccessToken;
    private GoogleApiClient mGoogleApiClient;
    private static final String TAG = "SignInActivity";
    private static final int RC_SIGN_IN = 9001;

    private FacebookCallback<LoginResult> mCallback=new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {

//            mTextDetail = (TextView) findViewById(R.id.editText);
//            mTextAccessToken = (TextView) findViewById(R.id.editText2);

            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();

            if(profile != null ){
//                mTextDetail.setText(profile.getFirstName());
//                mTextAccessToken.setText(profile.getLastName());

                Intent intent = new Intent(login.this,Home.class);
                startActivity(intent);

            }

        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException error) {

        }
    };





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

        Log.i("Looks like it work","Looks like it work");
        findViewById(R.id.sign_in_button).setOnClickListener(this);

        findViewById(R.id.sign_in_button).setOnClickListener(this);
        //findViewById(R.id.sign_out_button).setOnClickListener(this);
        //findViewById(R.id.disconnect_button).setOnClickListener(this);

        // [START configure_signin]
        // Configure sign-in to request the user's ID, email address, and basic
        // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();
        // [END configure_signin]

        // [START build_client]
        // Build a GoogleApiClient with access to the Google Sign-In API and the
        // options specified by gso.
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
        // [END build_client]

        // [START customize_button]
        // Customize sign-in button. The sign-in button can be displayed in
        // multiple sizes and color schemes. It can also be contextually
        // rendered based on the requested scopes. For example. a red button may
        // be displayed when Google+ scopes are requested, but a white button
        // may be displayed when only basic profile is requested. Try adding the
        // Scopes.PLUS_LOGIN scope to the GoogleSignInOptions to see the
        // difference.
        SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
        signInButton.setSize(SignInButton.SIZE_STANDARD);
        signInButton.setScopes(gso.getScopeArray());
        // [END customize_button]







        FacebookSdk.sdkInitialize(getApplicationContext());
        AppEventsLogger.activateApp(this);
        setContentView(R.layout.activity_login);

        mButton = (Button) findViewById(R.id.but_goToInterest);

        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(login.this, InterestScreen2.class);
                startActivity(intent);
//                Intent intent = new Intent(login.this,Home.class);
//                startActivity(intent);
            }
        });

        //Hide the Toolbar
        getSupportActionBar().hide();

        //Initialize Facebook SDK



        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("user_friends");
        callbackManager = CallbackManager.Factory.create();

        loginButton.registerCallback(callbackManager,mCallback);



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode,resultCode,data);
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sign_in_button:
                signIn();
                break;
            //case R.id.sign_out_button:
              //  signOut();
               // break;
            //case R.id.disconnect_button:
              //  revokeAccess();
               // break;
        }
    }
}

错误是:

  

引起:java.lang.NullPointerException:尝试调用虚拟   方法'无效   android.view.View.setOnClickListener(android.view.View $ OnClickListener)”   在空对象引用上

我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

在此声明之后

signInButton.setScopes(gso.getScopeArray());

为signInButton设置点击监听器

signInButton.setOnClickListener(this);

并在此声明之后

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

把这一个

 setContentView(R.layout.activity_login);

也删除这些陈述。

findViewById(R.id.sign_in_button).setOnClickListener(this);