我正在使用Firebase进行登录。我有一个使用电子邮件的用户-admin@admin.com,密码123456789。我想检查是否使用此帐户登录的用户已被授权删除数据。
String user = firebaseAuth.getCurrentUser().getUid();
intent.putExtra("name", user);
在MainActivity中,如果用户与他被授权的用户相同,则在显示祝酒词。
if(user.equals(intent.getStringExtra("name"))) {
Toast.makeText(MainActivity.this, "Authorized", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Not authorized", Toast.LENGTH_SHORT).show();
}
它总是返回一个空引用。
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean java.lang.String.equals(java.lang.Object)'
AdminActivity.java
firebaseAuth.signInWithEmailAndPassword(email,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
Log.d(TAG, "signInWithEmail:success");
String user = firebaseAuth.getCurrentUser().getUid();
Intent intent = new Intent(AdminActivity.this, MainActivity.class);
intent.putExtra("name", user);
startActivity(intent);
finish();
} else {
Log.w(TAG, "signInWithEmailFailure", task.getException());
Toast.makeText(AdminActivity.this, "Authentication Failed", Toast.LENGTH_LONG).show();
}
}
});
MainActivity.java
studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> parent, View view, int position, long id) {
selectedItem = studentDetails.get(position).getName();
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setPositiveButton("View", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, selectedItem, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
intent.putExtra("name", selectedItem);
startActivity(intent);
}
});
alertDialogBuilder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String user = null;
if(user.equals(intent.getStringExtra("name"))) {
Toast.makeText(MainActivity.this, "Authorized", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Not authorized", Toast.LENGTH_SHORT).show();
}
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});