0

I am working on a project, it has a simple log in and register screen. I have added a navigation drawer but the app crashes when i am trying to assign the textview value to be retrieved from the database or get it to display the logged in users email/username.

Drawerheader.xml:

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

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:src="@drawable/profile_pic" />

    <TextView
        android:id="@+id/Username"
        android:text="Username"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:layout_marginBottom="55dp"
        android:layout_width="75dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"/>

    <TextView
        android:id="@+id/textViewUserEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:padding="30dp"/>


</android.support.constraint.ConstraintLayout>

HomePageActivity.java:

public class HomePageActivity extends AppCompatActivity {

    private TextView textViewUserEmail;
    private TextView textUsername;
    private FirebaseAuth firebaseAuth;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private static final String TAG ="HomePageActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        Log.d(TAG,"onCreate:Started");

        //add the username and the email not sure if it is working yet since the database isn't set up yet.
        textViewUserEmail = (TextView) findViewById(R.id.textViewUserEmail);
        firebaseAuth = FirebaseAuth.getInstance();

        FirebaseUser user = firebaseAuth.getCurrentUser();
        //this crashes the app too, can not set the text to be anything
        textViewUserEmail.setText("Welcome  "+ user.getEmail());


        //DrawerMenu
        mDrawerLayout = (DrawerLayout) findViewById(R.id.homepage);//Assigning the variable and casting it, then saying where to find it.
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.closed);
        NavigationView nvDrawer = (NavigationView) findViewById(R.id.nv);

        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        setupDrawerContent(nvDrawer);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(mToggle.onOptionsItemSelected(item)){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    //Handling navigation view item clicks, every time  we check what item was clicked in the navigation drawer

    public void selectItemDrawer(MenuItem menuItem){
        Fragment myFragment = null;
        Class fragmentClass;
        switch (menuItem.getItemId()){
            case R.id.chat_room:
                fragmentClass = ChatRoom.class;
                break;
            case R.id.my_account:
                fragmentClass = MyAccount.class;
                break;
            case R.id.nav_settings:
                fragmentClass = Settings.class;
                break;
            case R.id.nav_logout:
                fragmentClass = null;
                firebaseAuth.signOut();
                finish();
                startActivity(new Intent(this, LoginActivity.class));
                /* this also crashes the app, i want the user to be logged out and return to the login page
                The loginIn page is not a fragment as it does not feature a main menu.
                firebaseAuth.signOut();
                finish();
                startActivity(new Intent(this, LoginActivity.class)); /or fragmentClass = LoginActivity.class;
                */
                break;
            default:
                fragmentClass = HomePageActivity.class;
        }
        //This will insert the fragment by replacing any existing fragment.
        try{
            myFragment = (Fragment) fragmentClass.newInstance();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        android.support.v4.app.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 false;
            }
        });
    }
}

Home page 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.brunelcs.group13.anyquestions.HomePageActivity"
    android:id="@+id/homepage">

    <FrameLayout
        android:id="@+id/flcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background">
    </FrameLayout>

    <android.support.design.widget.NavigationView
        app:headerLayout="@layout/drawerheader"
        android:id="@+id/nv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/colorPrimaryDark"
        app:itemTextColor="@color/white"
        app:menu="@menu/drawermenu"
        android:gravity="start">

    </android.support.design.widget.NavigationView>

The app crashes in the places commented by code. I am unable to change the textViewUserEmail not by setText to a simple "BOB" as the app crashes.

The second place where this happens is with the log out button. It should simply log the user out of the app and return to the LogIn activity.

Note that this activity is not a fragment as it is not featured in the main menu. Login an Register takes place prior.

What am i doing wrong? Is there anything i am missing or is this the wrong approach toward this?

this is the error:

 E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.brunelcs.group13.anyquestions, PID: 2626
                      java.lang.RuntimeException: Unable to start activity

ComponentInfo{com.brunelcs.group13.anyquestions/com.brunelcs.group13.anyquestions.HomePageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.brunelcs.group13.anyquestions.HomePageActivity.onCreate(HomePageActivity.java:40) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  Application terminated.

I know that i am pointing at null object since the home page uses the the drawerheader as headerlayout. But how can i access that drawer header to set the textview as whatever i need to?

1 Answers1

0

Are you sure you got Internet permission from AndroidManifest.xml?

<uses-permission android:name="android.permission.INTERNET" /> 

Put this line breakpoint and open it in debug mode. check if data is coming in.

FirebaseUser user = firebaseAuth.getCurrentUser();

you can check the comments on this link.

Erhan Biçer
  • 122
  • 6
  • Internet permissions are there, the app works perfectly fine, i have a activity that does not include the main menu and info is retrieved normally. – Bartosz Grabczak Nov 21 '17 at 22:25