当我尝试运行应用程序时,它会自动崩溃并显示错误
由于:java.lang.NullPointerException:尝试调用虚拟 方法'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()”在空对象上 参考 在com.example.akhilkumar.chitchat.MainActivity.onCreate(MainActivity.java:55)
指向
的错误currentUserID = mAuth.getCurrentUser()。getUid();
public class MainActivity extends AppCompatActivity {
private NavigationView navigationView;
private DrawerLayout drawerLayout;
private RecyclerView postList;
private Toolbar mToolbar;
private ActionBarDrawerToggle actionBarDrawerToggle;
private FirebaseAuth mAuth;
private DatabaseReference userRef;
private CircleImageView navProfileImage;
private TextView navProfileUserName;
String currentUserID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
userRef = FirebaseDatabase.getInstance().getReference().child("Users");
/*
mToolbar = (Toolbar)findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
*/
getSupportActionBar().setTitle("Home");
drawerLayout = (DrawerLayout)findViewById(R.id.drawable_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView = (NavigationView)findViewById(R.id.navigation_view);
View navView = navigationView.inflateHeaderView(R.layout.navigation_header);
navProfileImage = (CircleImageView)navView.findViewById(R.id.nav_profile_image);
navProfileUserName =(TextView)navView.findViewById(R.id.nav_user_full_name);
userRef.child(currentUserID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(dataSnapshot.exists())
{
if(dataSnapshot.hasChild("fullname"))
{
String fullname = dataSnapshot.child("fullname").getValue().toString();
navProfileUserName.setText(fullname);
}
if(dataSnapshot.hasChild("profileimage"))
{
String image = dataSnapshot.child("profileimage").getValue().toString();
Picasso.get().load(image).placeholder(R.drawable.profile).into(navProfileImage);
}
else
{
Toast.makeText(MainActivity.this, "Profile name do not exists...", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
UserMenuSelector(item);
return false;
}
});
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser == null){
SendUserToLoginActivity();
}
else{
CheckUserExistence();
}
}
private void CheckUserExistence() {
final String current_user_id = mAuth.getCurrentUser().getUid();
userRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(!dataSnapshot.hasChild(current_user_id))
{
SendUserToSetupActivity();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void SendUserToSetupActivity() {
Intent setupIntent = new Intent(MainActivity.this, SetupActivity.class);
setupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(setupIntent);
finish();
}
private void SendUserToLoginActivity() {
Intent loginIntenet = new Intent(MainActivity.this, LoginActivity.class);
loginIntenet.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntenet);
finish();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(actionBarDrawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
private void UserMenuSelector(MenuItem item) {
switch (item.getItemId()){
case R.id.nav_profile:
Toast.makeText(this,"Profile",Toast.LENGTH_LONG).show();
break;
case R.id.nav_home:
Toast.makeText(this,"Home",Toast.LENGTH_LONG).show();
break;
case R.id.nav_friends:
Toast.makeText(this,"Friends",Toast.LENGTH_LONG).show();
break;
case R.id.nav_find_friends:
Toast.makeText(this,"Find Friends",Toast.LENGTH_LONG).show();
break;
case R.id.nav_messages:
Toast.makeText(this,"Messages",Toast.LENGTH_LONG).show();
break;
case R.id.nav_settings:
Toast.makeText(this,"Settings",Toast.LENGTH_LONG).show();
break;
case R.id.nav_logout:
mAuth.signOut();
SendUserToLoginActivity();
Toast.makeText(this,"Logout Successfully",Toast.LENGTH_LONG).show();
break;
case R.id.nav_about_app:
Toast.makeText(this,"About",Toast.LENGTH_LONG).show();
break;
}
}
}
答案 0 :(得分:1)
下面的代码行:
currentUserID = mAuth.getCurrentUser().getUid();
可能产生NullPointerException
。因此,要解决此问题,您需要首先检查无效性。因此,请使用以下代码行:
FirebaseUser mFirebaseUser = mAuth.getCurrentUser();
if(mFirebaseUser != null) {
currentUserID = mFirebaseUser.getUid(); //Do what you need to do with the id
}
答案 1 :(得分:1)
您正在onStart方法中访问mAuth,但正在onCreate中对其进行了初始化。因此,这就是您收到此错误的原因。
因此,请在onCreate()方法中编写以下代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser == null){
SendUserToLoginActivity();
}
else{
CheckUserExistence();
}
}