Я пытался создать код Uid
и столкнулся с этой проблемой по поводу NullPointException
:
package com.example.musicmates;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.database.annotations.NotNull;
import com.squareup.picasso.Picasso;
import java.util.Set;
import de.hdodenhof.circleimageview.CircleImageView;
public class MainActivity extends AppCompatActivity {
private NavigationView navigationView;
private DrawerLayout drawerLayout;
private RecyclerView postList;
private Toolbar mToolbar;
private CircleImageView NavProfileImage;
private TextView NavProfileUserName;
private ActionBarDrawerToggle actionBarDrawerToggle;
private FirebaseAuth mAuth;
private DatabaseReference UsersRef;
String currentUserID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigationView = findViewById(R.id.navigaition_view);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
mToolbar = findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Home");
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);
View navView = navigationView.inflateHeaderView(R.layout.navigation_header);
NavProfileImage = navView.findViewById(R.id.nav_profile_image);
NavProfileUserName = navView.findViewById(R.id.nav_profile_full_name);
UsersRef.child(currentUserID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
String fullname = dataSnapshot.child("fullname").getValue().toString();
String image = dataSnapshot.child("profileimage").getValue().toString();
NavProfileUserName.setText(fullname);
Picasso.get().load(image).placeholder(R.drawable.profile).into(NavProfileImage);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
UserMenuSelector(menuItem);
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();
UsersRef.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 loginIntent = new Intent(MainActivity.this,LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
finish();
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if(actionBarDrawerToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
private void UserMenuSelector(MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.nav_post:
Toast.makeText(this, "Add new post", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_profile:
Toast.makeText(this, "Profile", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_home:
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_friends:
Toast.makeText(this, "Friends", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_find_friends:
Toast.makeText(this, "Find Friends", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_message:
Toast.makeText(this, "Message", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_setting:
Toast.makeText(this, "Setting", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_logout:
Toast.makeText(this, "Log out", Toast.LENGTH_SHORT).show();
mAuth.signOut();
SendUserToLoginActivity();
break;
}
}
}
2019-11-02 11: 46: 36.860 16810-16810 / com.example.musicmates E / AndroidRuntime: ИСКЛЮЧИТЕЛЬНОЕ ИСКЛЮЧЕНИЕ: основной Процесс: com.example.musicmates, PID: 16810 java.lang.RuntimeException: Невозможно запустить действие ComponentInfo {com.example.musicmates / com.example.musicmates.MainActivity}:java.lang.NullPointerException: попытка вызвать виртуальный метод 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid ()' для ссылки на пустой объект в android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2817). ) на android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2892) на android.app.ActivityThread.-wrap11 (неизвестный источник: 0) на android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1593) на android.os.Handler.dispatchMessage (Handler.java:105) при android.os.Looper.loop (Looper.java:164) в android.app.ActivityThread.main (ActivityThread.java:6541) в java.lang.reflect.Method.invoke (собственный метод) на com.android.internal.os.Zygote $ MethodAndArgsCaller.run (Zygote.java:240) на com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767). Причинаby: java.lang.NullPointerException: попытка вызвать виртуальный метод 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid ()' для пустой ссылки на объект в com.example.musicmates.MainActivity.onCreate (MainActivity.java: 56) в android.app.Activity.performCreate (Activity.java:6975) в android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1213) в android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2770)