-2

Here is my SignInActivityCode

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

GoogleApiClient mGoogleApiClient;
private static final int RC_SIGN_IN=0;
private String TAG = "SignInActivity";

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

    try{
        getSupportActionBar().hide();
    }catch (Exception e){
        e.printStackTrace();
    }

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

    SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
    signInButton.setSize(SignInButton.SIZE_STANDARD);
    signInButton.setOnClickListener(this);

}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

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

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


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

    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN){
        GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
        handleSignInIntent(result);
    }
}

private void handleSignInIntent(GoogleSignInResult result){
    Log.d(TAG, "handleSignInResult:" + result.isSuccess());
    if(result.isSuccess()){
        GoogleSignInAccount acct = result.getSignInAccount();

        Toast.makeText(this, "Sign in successful!", Toast.LENGTH_LONG).show();
        setDefaults(acct.getDisplayName(), acct.getEmail(), acct.getIdToken());
        Intent intent = new Intent(SignInActivity.this, MainActivity.class);
        startActivity(intent);
    }
    else{
        Toast.makeText(this, "Sign In Failed!\nPlease try again.", Toast.LENGTH_LONG).show();
    }
}

public void setDefaults(String name, String email, String token){
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString("userName", name);
    editor.putString("userEmail", email);
    editor.putString("userIdToken", token);
    editor.apply();
}

}

and here is how I am applying the information passed to the MainActivity in my nav_header_main.xml file

@Override
protected void onCreate(Bundle savedInstanceState) {
 initializeUserInfo();
}
private void initializeUserInfo() {

    userName = (TextView) findViewById(R.id.nav_user_name_tv);
    userEmail = (TextView) findViewById(R.id.nav_user_email_tv);

    final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

    name = sharedPref.getString("userName", "");
    email = sharedPref.getString("userEmail", "");

    try{
        userName.setText(name);
        userEmail.setText(email);
    }catch(Exception e){
        e.printStackTrace();
    }
}

and the error that I am getting in the logs

Blockquote java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Gautam Hans
  • 135
  • 1
  • 12

1 Answers1

1

This is how you initialize and declare navigation view items

private NavigationView navigationView;
private DrawerLayout drawer;
private View navHeader;

drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);

// Navigation view header
navHeader = navigationView.getHeaderView(0);
txtName = (TextView) navHeader.findViewById(R.id.nameNav);
Nirup Iyer
  • 1,215
  • 2
  • 14
  • 20