Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using firebase as my database for my app which is an chatting app. And my app is crashing whenever I try to login or sign up to my app main activity the app crashes automatically.

MainActivity.java
Java
package com.vortexv.vconnect;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.firebase.Firebase;
import com.google.firebase.auth.FirebaseAuth;
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 java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    FirebaseAuth auth;
    RecyclerView mainUserRecyclerView;
    UserAdapter adapter;
    FirebaseDatabase database;
    ArrayList<Users> usersArrayList;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();

        database = FirebaseDatabase.getInstance();
        auth = FirebaseAuth.getInstance();
        DatabaseReference reference = database.getReference().child("user");
        usersArrayList = new ArrayList<>();

        mainUserRecyclerView = (RecyclerView)findViewById(R.id.mainUserRecyclerView);
        mainUserRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new UserAdapter(MainActivity.this, usersArrayList);
        mainUserRecyclerView.setAdapter(adapter);

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot: snapshot.getChildren())
                {
                    Users users = dataSnapshot.getValue(Users.class);
                    usersArrayList.add(users);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });




        if (auth.getCurrentUser() == null) {
            Intent intent = new Intent(MainActivity.this, Login.class);
            startActivity(intent);
        }
    }
}


Users.java
Java
package com.vortexv.vconnect;

public class Users {
    String profilepic,mail,userName,password,userID,lastMessage,status;

    public Users(String id, String namee, String emaill, String password, String imageuri, String status){
        this.userID = id;
        this.userName = namee;
        this.mail = emaill;
        this.password = password;
        this.profilepic = imageuri;
        this.status = status;
    }

    public String getProfilepic() {
        return profilepic;
    }

    public void setProfilepic(String profilepic) {
        this.profilepic = profilepic;
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserID() {
        return userID;
    }

    public void setUserID(String userID) {
        this.userID = userID;
    }

    public String getLastMessage() {
        return lastMessage;
    }

    public void setLastMessage(String lastMessage) {
        this.lastMessage = lastMessage;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
}


MainActivity.xml

XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_bg"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/toolBar"
        android:layout_width="wrap_content"
        android:layout_height="63dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="23dp"
        android:layout_marginTop="@dimen/_24sdp"
        android:layout_marginEnd="25dp"
        android:background="@color/white"
        android:gravity="right"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="23dp"
        android:layout_marginRight="25dp">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="@dimen/_10sdp"
            android:layout_marginLeft="@dimen/_10sdp"
            android:layout_weight="1"
            android:fontFamily="@font/poppins_semibold"
            android:text="Vortex Connect"
            android:textColor="@color/black"
            android:textSize="@dimen/_20sdp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/logout_img"
            android:layout_width="@dimen/_50sdp"
            android:layout_height="@dimen/_50sdp"
            android:layout_gravity="center"
            android:layout_marginRight="@dimen/_10sdp"
            android:layout_weight="1"
            app:srcCompat="@drawable/baseline_logout_24" />

    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/mainUserRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="369dp"
        android:layout_below="@id/toolBar"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="7dp"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="159dp"
        android:layout_marginEnd="18dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="114dp" />

    <ImageView
        android:layout_width="@dimen/_32sdp"
        android:layout_height="@dimen/_32sdp"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="@dimen/_72sdp"
        android:layout_marginRight="270dp"
        android:layout_marginBottom="@dimen/_32sdp"
        android:src="@drawable/camera_white_icon" />

    <ImageView
        android:layout_width="@dimen/_32sdp"
        android:layout_height="@dimen/_32sdp"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="@dimen/_138sdp"
        android:layout_marginRight="193dp"
        android:layout_marginBottom="@dimen/_32sdp"
        android:src="@drawable/chats_white_icon" />

    <ImageView
        android:layout_width="@dimen/_32sdp"
        android:layout_height="@dimen/_32sdp"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="@dimen/_200sdp"
        android:layout_marginRight="113dp"
        android:layout_marginBottom="@dimen/_32sdp"
        android:src="@drawable/settings_white_icon" />
</RelativeLayout>


Stack Trace
FATAL EXCEPTION: main                                                                                                 Process: com.vortexv.vconnect, PID: 28436
                                                                                                    com.google.firebase.database.DatabaseException: Class com.vortexv.vconnect.Users does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
                                                                                                        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:578)
                                                                                                        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:571)
                                                                                                        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433)
                                                                                                        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
                                                                                                        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
                                                                                                        at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:202)
                                                                                                        at com.vortexv.vconnect.MainActivity$1.onDataChange(MainActivity.java:56)
                                                                                                        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
                                                                                                        at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
                                                                                                        at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:942)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:211)
                                                                                                        at android.os.Looper.loop(Looper.java:300)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8348)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:582)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1028)


What I have tried:

Logs provided if its helped:
<a href="https://pastebin.com/M9GUQC6k"></a>link - cuz its showing spam on posting page

I am watching a tutorial on youtube to make this app. But his code is working but not mine even i have checked the code thrice.

My app is crashing on my main Activity which is my chat home screen.

I have tired to rewrite the code but my problem is not solving. Please help!!
Posted

1 solution

Add an empty default constructor to the class.
Java
public Users() {}
The error told you exactly what the problem was.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900