Nant Nant - 7 months ago 21
Java Question

Getting error on creating Google Login

I am trying to implement the following google login and I am getting the following error:

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


The error is:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

What am I doing wrong here ?

Answer

After this statement

signInButton.setScopes(gso.getScopeArray());

put a click listener for signInButton

signInButton.setOnClickListener(this);

and after this statement

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

put this one

 setContentView(R.layout.activity_login);

remove these statements as well.

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