简体   繁体   English

登录后在导航抽屉中显示电子邮件

[英]Displaying email in navigation drawer after logging in

I am trying to show email from login.java page in the navigation drawer after they login to Android App.我试图在他们登录到 Android 应用程序后在导航抽屉中显示来自 login.java 页面的电子邮件。 I am getting a FATAL EXCEPTION error and java.lang.NullPointerException when I run.我在运行时收到 FATAL EXCEPTION 错误和 java.lang.NullPointerException。

I have put code into DoctorHome.java:我已将代码放入 DoctorHome.java:

package com.example.medicationmanagementsystem;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import com.example.medicationmanagementsystem.DAO.DatabaseHelper;
import com.google.android.material.navigation.NavigationView;

public class DoctorHome extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    private DrawerLayout drawer;
    TextView txtDisplayEmail;
    DatabaseHelper myDb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.doctor_layout);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        drawer = findViewById(R.id.drawer_layout);

        txtDisplayEmail = findViewById(R.id.emaildisplay);
        txtDisplayEmail.setText(myDb.getEmail());

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();
    }
    //END

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()) {
            case R.id.nav_createprescription:
                Intent createpresintent = new Intent(DoctorHome.this, MainActivity.class);
                startActivity(createpresintent);
                break;
            case R.id.viewprescriptions:
                Intent presintent = new Intent(DoctorHome.this, ViewListContents.class);
                startActivity(presintent);
                break;
            case R.id.nav_addpatients:
                Intent createpatientintent = new Intent(DoctorHome.this, NewPatient.class);
                startActivity(createpatientintent);
                break;
            case R.id.nav_viewpatients:
                Intent viewpatientintent = new Intent(DoctorHome.this, ViewPatientListContents.class);
                startActivity(viewpatientintent);
                break;
            case R.id.nav_logout:
                Intent logoutintent= new Intent(DoctorHome.this, Login.class);
                startActivity(logoutintent);
                break;
        }
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onBackPressed() {
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }
}

DatabaseHelper.java数据库助手

//getting email
    public String getEmail() throws SQLException {
        String email = "";
        Cursor cursor = this.getReadableDatabase().query(
                TABLE_USERS, new String[] {COL_USER_EMAIL},
                null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                email = cursor.getString(0);
            } while (cursor.moveToNext());
        }
        cursor.close();

        return email;
    }

I am using SQLite as the database to save user details.我使用 SQLite 作为数据库来保存用户详细信息。 I just need the email from this to display in the textview emaildisplay in the navigation header.我只需要来自此的电子邮件显示在导航标题中的 textview emaildisplay 中。 Any help would be appreciated.任何帮助,将不胜感激。 Thank you!!谢谢!!

First you have to initialize myDb before using it like this:首先,您必须在使用myDb之前初始化它,如下所示:

myDb = new DatabaseHelper(this);

Also txtDisplayEmail is not inside your activity's layout, so this:此外txtDisplayEmail不在您的活动布局内,因此:

txtDisplayEmail = findViewById(R.id.emaildisplay);

returns null .返回null
What you should do is find txtDisplayEmail inside the NavigationView 's header.您应该做的是在NavigationView的标题中找到txtDisplayEmail
Change to this:改成这样:

myDb = new DatabaseHelper(this);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
View header = navigationView.getHeaderView(0);
txtDisplayEmail = (TextView) header.findViewById(R.id.emaildisplay);
txtDisplayEmail.setText(myDb.getEmail());
navigationView.setNavigationItemSelectedListener(this);


But the method getEmail() of DatabaseHelper does not return the user's email (if this is what it is supposed to do).但是DatabaseHelper getEmail()方法不会返回用户的电子邮件(如果这是它应该做的)。
It just loops over all the emails of the table and returns the last one.它只是遍历表格中的所有电子邮件并返回最后一封。
So change it to return the email of the logged in user.因此将其更改为返回登录用户的电子邮件。

instead of findViewById(R.id.emaildisplay);而不是findViewById(R.id.emaildisplay);

try navigationView.findViewById(R.id.emaildisplay);尝试navigationView.findViewById(R.id.emaildisplay);

I assume emaildisplay is part of nvigationView not the activity layout.我认为 emaildisplay 是 nvigationView 的一部分,而不是活动布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM