我试图在登录 Android 应用程序后在抽屉导航中显示来自 login.java 页面的电子邮件。运行时出现 FATAL EXCEPTION 错误和 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 电子邮件显示中。任何帮助,将不胜感激。谢谢你!!
最佳答案
首先,您必须先初始化myDb
,然后才能像这样使用它:
myDb = new DatabaseHelper(this);
此外,txtDisplayEmail
不在您的 Activity 布局内,因此:
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()
不会返回用户的电子邮件(如果这是它应该做的)。
它只是循环遍历表中的所有电子邮件并返回最后一封。
因此,将其更改为返回登录用户的电子邮件。
关于java - 登录后在抽屉导航中显示电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59992749/