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

Java 登录后在导航抽屉中显示电子邮件,java,android,sqlite,Java,Android,Sqlite,在他们登录Android应用程序后,我试图在导航抽屉中的login.java页面显示电子邮件。当我运行时,我得到一个致命的异常错误和java.lang.NullPointerException 我已将代码放入DoctorHome.java中: package com.example.medicationmanagementsystem; import android.content.Intent; import android.os.Bundle; import android.view.Me

在他们登录Android应用程序后,我试图在导航抽屉中的login.java页面显示电子邮件。当我运行时,我得到一个致命的异常错误和java.lang.NullPointerException

我已将代码放入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;
    }
我使用SQLite作为数据库来保存用户详细信息。我只需要将此邮件显示在导航标题的textview emaildisplay中。任何帮助都将不胜感激。谢谢

而不是findviewbydr.id.emaildisplay

尝试navigationView.findViewByIdR.id.emaildisplay

我假设emaildisplay是nvigationView的一部分,而不是活动布局。

而不是findViewByIdR.id.emaildisplay

尝试navigationView.findViewByIdR.id.emaildisplay


我假设emaildisplay是nvigationView的一部分,而不是活动布局。

首先,您必须初始化myDb,然后才能像这样使用它:

myDb = new DatabaseHelper(this);
此外,txtDisplayEmail不在活动的布局内,因此:

txtDisplayEmail = findViewById(R.id.emaildisplay);
返回null。 您应该做的是在NavigationView的标题中查找txtDisplayEmail。 更改为:

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);
但是DatabaseHelper的getEmail方法不会返回用户的电子邮件,如果这是它应该做的。 它只是循环遍历表中的所有电子邮件并返回最后一封。
因此,将其更改为返回登录用户的电子邮件

在像这样使用myDb之前,首先必须初始化它:

myDb = new DatabaseHelper(this);
此外,txtDisplayEmail不在活动的布局内,因此:

txtDisplayEmail = findViewById(R.id.emaildisplay);
返回null。 您应该做的是在NavigationView的标题中查找txtDisplayEmail。 更改为:

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);
但是DatabaseHelper的getEmail方法不会返回用户的电子邮件,如果这是它应该做的。 它只是循环遍历表中的所有电子邮件并返回最后一封。
因此,将其更改为返回登录用户的电子邮件

您忘记初始化myDb:myDb=newdatabasehelper此;谢谢,但不幸的是,仍然会出现相同的错误!发布错误日志。E/AndroidRuntime:致命异常:主进程:com.example.medicationmanagementsystem,PID:1494 java.lang.RuntimeException:无法启动活动组件信息{com.example.medicationmanagementsystem/com.example.medicationmanagementsystem.DoctorHome}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setTextjava.lang.CharSequence'。您忘记初始化myDb:myDb=new DatabaseHelper此;谢谢,但不幸的是,仍然会出现相同的错误!发布错误日志。E/AndroidRuntime:致命异常:主进程:com.example.medicationmanagementsystem,PID:1494 java.lang.RuntimeException:无法启动活动组件信息{com.example.medicationmanagementsystem/com.example.medicationmanagementsystem.DoctorHome}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setTextjava.lang.CharSequence'。非常感谢,txtemaildisplay现在正在显示一封电子邮件,但您是对的,它只是使用在数据库中注册的最后一封电子邮件。非常感谢,txtemaildisplay现在正在显示一封电子邮件,但您是对的,它只是使用数据库中注册的最后一封电子邮件。