java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()
при открытии приложения получаю ошибку :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.bred.forum16, PID: 6275
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bred.forum16/com.bred.forum16.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3556)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2216)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
at com.bred.forum16.MainActivity.onCreate(MainActivity.java:69)
at android.app.Activity.performCreate(Activity.java:7955)
at android.app.Activity.performCreate(Activity.java:7944)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3531)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3703)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2216)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
MainActivtity.java
public class MainActivity extends AppCompatActivity
{
private NavigationView navigationView;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private RecyclerView postList;
private Toolbar mToolbar;
private CircleImageView NavProfileImage;
private TextView NavProfileUserName;
private ImageButton AddNewPostButton;
private FirebaseAuth mAuth;
private DatabaseReference UsersRef, PostsRef, LikesRef;
String currentUserID;
Boolean LikeChecker = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
PostsRef = FirebaseDatabase.getInstance().getReference().child("Posts");
LikesRef = FirebaseDatabase.getInstance().getReference().child("Likes");
mToolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("Home");
AddNewPostButton = (ImageButton) findViewById(R.id.add_new_post_button);
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);
postList = (RecyclerView) findViewById(R.id.all_users_post_list);
postList.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
postList.setLayoutManager(linearLayoutManager);
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);
UsersRef.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, "profilename 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;
}
});
AddNewPostButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
SendUserToPostActivity();
}
});
DisplayAllUsersPosts();
}
public void updateUserStatus(String state)
{
String saveCurrentDate, saveCurrentTime;
Calendar calForDate = Calendar.getInstance();
SimpleDateFormat currentDate = new SimpleDateFormat("dd-MMM-YYYY");
saveCurrentDate = currentDate.format(calForDate.getTime());
Calendar calForTime = Calendar.getInstance();
SimpleDateFormat currentTime = new SimpleDateFormat("hh:mm:ss a");
saveCurrentTime = currentTime.format(calForTime.getTime());
Map currentStateMap = new HashMap();
currentStateMap.put("time", saveCurrentTime);
currentStateMap.put("date", saveCurrentDate);
currentStateMap.put("type", state);
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null)
{
UsersRef.child(currentUserID).child("userState").updateChildren(currentStateMap);
}
}
private void DisplayAllUsersPosts()
{
Query SortPostsInDecendingOrder = PostsRef.orderByChild("counter");
FirebaseRecyclerOptions<Posts> options = new FirebaseRecyclerOptions.Builder<Posts>().setQuery(SortPostsInDecendingOrder, Posts.class).build();
FirebaseRecyclerAdapter<Posts, PostsViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Posts, PostsViewHolder>(options)
{
@NonNull
@Override
public PostsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
{
View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.all_posts_layout, viewGroup, false);
PostsViewHolder viewHolder=new PostsViewHolder(view);
return viewHolder;
}
@Override
protected void onBindViewHolder(@NonNull PostsViewHolder holder, int position, @NonNull Posts model)
{
final String PostKey = getRef(position).getKey();
holder.username.setText(model.getFullname());
holder.time.setText(" Time: " +model.getTime());
holder.date.setText("Date: " +model.getDate());
holder.description.setText(model.getDescription());
Picasso.get().load(model.getProfileimage()).into(holder.user_post_image);
Picasso.get().load(model.getPostimage()).into(holder.postImage);
holder.setLikeButtonStatus(PostKey);
holder.mView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View v)
{
Intent clickPostIntent = new Intent(MainActivity.this, ClickPostActivity.class);
clickPostIntent.putExtra("PostKey", PostKey);
startActivity(clickPostIntent);
}
});
holder.CommentPostButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent commentsIntent = new Intent(MainActivity.this, CommentsActivity.class);
commentsIntent.putExtra("PostKey", PostKey);
startActivity(commentsIntent);
}
});
holder.LikepostButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
LikeChecker = true;
LikesRef.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
if(LikeChecker.equals(true))
{
if(dataSnapshot.child(PostKey).hasChild(currentUserID))
{
LikesRef.child(PostKey).child(currentUserID).removeValue();
LikeChecker = false;
}
else
{
LikesRef.child(PostKey).child(currentUserID).setValue(true);
LikeChecker = false;
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
}
});
}
};
postList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
public static class PostsViewHolder extends RecyclerView.ViewHolder
{
TextView username, date, time, description;
CircleImageView user_post_image;
ImageView postImage;
View mView;
ImageButton LikepostButton, CommentPostButton;
TextView DisplayNoOfLikes;
int countLikes;
String currentUserId;
DatabaseReference LikesRef;
public PostsViewHolder(@NonNull View itemView)
{
super(itemView);
mView = itemView;
LikepostButton = (ImageButton) mView.findViewById(R.id.like_button);
CommentPostButton = (ImageButton) mView.findViewById(R.id.comment_button);
DisplayNoOfLikes = (TextView) mView.findViewById(R.id.display_no_of_likes);
LikesRef = FirebaseDatabase.getInstance().getReference().child("Likes");
currentUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
username=itemView.findViewById(R.id.post_user_name);
date=itemView.findViewById(R.id.post_date);
time=itemView.findViewById(R.id.post_time);
description=itemView.findViewById(R.id.post_description);
postImage=itemView.findViewById(R.id.post_image);
user_post_image=itemView.findViewById(R.id.post_profile_image);
}
public void setLikeButtonStatus(final String PostKey)
{
LikesRef.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
if(dataSnapshot.child(PostKey).hasChild(currentUserId))
{
countLikes = (int) dataSnapshot.child(PostKey).getChildrenCount();
LikepostButton.setImageResource(R.drawable.like);
DisplayNoOfLikes.setText((Integer.toString(countLikes) + (" Likes")));
}
else
{
countLikes = (int) dataSnapshot.child(PostKey).getChildrenCount();
LikepostButton.setImageResource(R.drawable.dislike);
DisplayNoOfLikes.setText((Integer.toString(countLikes) + (" Likes")));
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
}
}
@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(@NonNull DataSnapshot dataSnapshot)
{
if(!dataSnapshot.hasChild(current_user_id))
{
SendUserToSetupActivity();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
}
private void UserMenuSelector(MenuItem item)
{
switch(item.getItemId())
{
case R.id.nav_post:
SendUserToPostActivity();
break;
case R.id.nav_profile:
SendUserToProfileActivity();
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:
SendUserToFriendsActivity();
Toast.makeText(this, "Friend List", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_find_friends:
SendUserToFindfriendsActivity();
Toast.makeText(this, "Find Friends", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_messages:
SendUserToFriendsActivity();
Toast.makeText(this, "Message", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_settings:
SendUserToSettingActivity();
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
break;
case R.id.nav_Logout:
updateUserStatus("offline");
mAuth.signOut();
SendUserToLoginActivity();
break;
}
}
private void SendUserToFriendsActivity()
{
Intent friendsIntent = new Intent(MainActivity.this, FriendsActivity.class);
startActivity(friendsIntent);
}
private void SendUserToSettingActivity()
{
Intent loginIntent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(loginIntent);
}
private void SendUserToFindfriendsActivity()
{
Intent loginIntent = new Intent(MainActivity.this, FindFriendsActivity.class);
startActivity(loginIntent);
}
private void SendUserToProfileActivity()
{
Intent loginIntent = new Intent(MainActivity.this, ProfileActivity.class);
startActivity(loginIntent);
}
private void SendUserToPostActivity()
{
Intent addNewPostIntent = new Intent(MainActivity.this, PostActivity.class);
startActivity(addNewPostIntent);
}
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(MenuItem item)
{
if(actionBarDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bred.forum16">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Forum16">
<activity android:name=".ChatActivity" />
<activity android:name=".ClickPostActivity" />
<activity android:name=".CommentsActivity" />
<activity android:name=".FindFriendsActivity" />
<activity android:name=".FriendsActivity" />
<activity android:name=".LoginActivity" />
<activity android:name=".MyPostsActivity" />
<activity android:name=".PersonProfileActivity" />
<activity android:name=".PostActivity" />
<activity android:name=".ProfileActivity" />
<activity android:name=".RegisterActivity" />
<activity android:name=".ResetPasswordActivity" />
<activity android:name=".SettingsActivity" />
<activity android:name=".SetupActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>