-2

new to Android Studio. I encountered a problem when testing my application. After signing in using Google Sign-In it would crash saying "Unfortunately, Aisier has stopped." I am trying to direct the user after signing in to a Navigation Drawer which also has their information such as profile picture, name and email. Am I missing some code here?

My Navigation.java:

package com.example.android.aisier;

import android.content.Intent;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.constraint.solver.SolverVariable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.OptionalPendingResult;
import com.google.android.gms.common.api.ResultCallback;

public class Navigation extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private ImageView photoImageView;
    private TextView nameTextView;
    private TextView emailTextView;
    private GoogleApiClient googleApiClient;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        NavigationView nvDrawer = (NavigationView) findViewById(R.id.nv);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setupDrawerContent(nvDrawer);

        photoImageView = (ImageView) findViewById(R.id.photoImageView);
        nameTextView = (TextView) findViewById(R.id.nameTextView);
        emailTextView = (TextView) findViewById(R.id.nameTextView);

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

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

    @Override
    protected void onStart() {
        super.onStart();

        OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(googleApiClient);
        if (opr.isDone()) {
            GoogleSignInResult result = opr.get();
            handleSignInResult(result);
        } else {
            opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
                @Override
                public void onResult(@NonNull GoogleSignInResult googleSignInResult) {
                    handleSignInResult(googleSignInResult);
                }
            });
        }
    }

    private void handleSignInResult(GoogleSignInResult result) {
        if (result.isSuccess()) {

            GoogleSignInAccount account = result.getSignInAccount();
            nameTextView.setText(account.getDisplayName());
            emailTextView.setText(account.getEmail());

            Glide.with(this).load(account.getPhotoUrl()).into(photoImageView);

        } else {
            Intent intent = new Intent(this, SignIn.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public void selectItemDrawer(MenuItem menuItem) {
        Fragment myFragment = null;
        Class fragmentClass;
        switch (menuItem.getItemId()) {
            case R.id.db:
                fragmentClass = Dashboard.class;
                break;
            case R.id.converter:
                fragmentClass = Converter.class;
                break;
            case R.id.location:
                fragmentClass = Location.class;
                break;
            case  R.id.translator:
                fragmentClass = Translate.class;
                break;
            case  R.id.memo:
                fragmentClass = Memo.class;
                break;
            default:
                fragmentClass = Dashboard.class;
        }
        try {
            myFragment = (Fragment) fragmentClass.newInstance();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.flcontent,myFragment).commit();
        menuItem.setChecked(true);
        setTitle(menuItem.getTitle());
        mDrawerLayout.closeDrawers();
    }
    private void setupDrawerContent(NavigationView navigationView) {
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                selectItemDrawer(item);
                return true;
            }
        });
    }

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

    }
}

My activity_navigation.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.aisier.Navigation">
    <FrameLayout
        android:id="@+id/flcontent"
        android:background="@drawable/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nv"
        app:headerLayout="@layout/header"
        android:layout_width="264dp"
        android:layout_height="match_parent"
        android:background="@color/white"
        app:itemTextColor="@color/darkgrey"
        app:itemIconTint="@color/darkgrey"
        app:menu="@menu/navigationmenu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

My header.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="160dp"
    android:background="@drawable/header"
    android:padding="20dp">

    <ImageView
        android:id="@+id/photoImageView"
        android:layout_width="75dp"
        android:layout_height="75dp" />
    <TextView
        android:id="@+id/nameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:layout_marginTop="5dp"/>
    <TextView
        android:id="@+id/emailTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="16dp"
        />

</LinearLayout>

My LogCat:

10-29 09:04:42.606 4859-4859/com.example.android.aisier E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.aisier, PID: 4859 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.android.aisier.Navigation.handleSignInResult(Navigation.java:85) at com.example.android.aisier.Navigation.access$000(Navigation.java:30) at com.example.android.aisier.Navigation$1.onResult(Navigation.java:75) at com.example.android.aisier.Navigation$1.onResult(Navigation.java:72) at com.google.android.gms.internal.zzqq$zza.zzb(Unknown Source) at com.google.android.gms.internal.zzqq$zza.handleMessage(Unknown Source) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Any help would be appreciated!

Cwqlei
  • 1
  • 1
  • you have assigned wrong id to email text view in Activity. – Sanjeet Oct 29 '17 at 09:26
  • Since those `TextView`s are in a `NavigationView` header set in the layout, you need to find them a little differently in `onCreate()`. [This answer](https://stackoverflow.com/a/33699825) shows exactly how to do that. Also, as Sanjeet points out, you've used the same ID in finding both, but that wouldn't cause the crash. It'd just set the texts incorrectly. – Mike M. Oct 29 '17 at 09:36

1 Answers1

0

You have assigned wrong id to email text view in Activity.

Change

emailTextView = (TextView) findViewById(R.id.nameTextView);

to

emailTextView = (TextView) findViewById(R.id.emailTextView);
Sanjeet
  • 2,385
  • 1
  • 13
  • 22