Home > Mobile >  Why does my app crash when firebase rules are set to true?
Why does my app crash when firebase rules are set to true?

Time:02-15

I have made an app that retrieves data from a firebase real-time database and displays it in a RecyclerView.

MainActivity.java:

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.widget.Button;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.widget.TextView;

import com.google.firebase.FirebaseApp;
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 {

    RecyclerView recyclerview;
    ArrayList<Dishes> list;
    DatabaseReference reference;
    Food_RecyclerViewAdapter adapter;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerview = findViewById(R.id.recycler);
        reference = FirebaseDatabase.getInstance().getReference("Dishes");
        recyclerview.setHasFixedSize(true);
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        button = findViewById(R.id.button);

        list = new ArrayList<>();
        adapter = new Food_RecyclerViewAdapter(this, list);
        recyclerview.setAdapter(adapter);


        reference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull  DataSnapshot snapshot) {

                        for (DataSnapshot ds : snapshot.getChildren()){
                            //Add firebase data to Recyclerview adapter
                            Dishes dish = ds.getValue(Dishes.class);
                            list.add(dish);

                        }
                        adapter.notifyDataSetChanged();

                    }

                    @Override
                    //when the connection is cancelled call the Display() method
                    public void onCancelled(@NonNull  DatabaseError error) {
                        Display();
                    }
                });
        }

    private void Display() {
//Display the toast message "Error"
        Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
    }
}

The Model class for the RecyclerView:

package com.example.myapplication;

class Dishes {
    String name;
    int price;
    float rating;


    public Dishes(String name, float rating, int price) {
        this.name = name;
        this.price = price;
        this.rating = rating;
    }

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }

    public float getRating() {
        return rating;
    }


}

The RecyclerView adapter:

package com.example.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

class Food_RecyclerViewAdapter extends RecyclerView.Adapter<Food_RecyclerViewAdapter.MyViewHolder> {
    Context context;
    ArrayList<Dishes> models;


    public Food_RecyclerViewAdapter(Context context, ArrayList<Dishes> models){
        this.context = context;
        this.models = models;
    }

    @NonNull
    @Override
    public Food_RecyclerViewAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.item, parent, false);

        return new Food_RecyclerViewAdapter.MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Food_RecyclerViewAdapter.MyViewHolder holder, int position) {
        Dishes dish = models.get(position);

        holder.name.setText(dish.getName());
        holder.price.setText("Price"   " $ "   dish.getPrice());
        holder.rating.setRating(dish.getRating());

    }

    @Override
    public int getItemCount() {
        return models.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView name, price;
        Button button;
        RatingBar rating;

        public MyViewHolder(@NonNull View ItemView) {
            super(ItemView);

            name = ItemView.findViewById(R.id.food_name);
            price = ItemView.findViewById(R.id.pri);
            button = ItemView.findViewById(R.id.btn);
            rating = ItemView.findViewById(R.id.ratingBar);
        }
    }
}

Dependencies:

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.firebase:firebase-database:20.0.3'
    testImplementation 'junit:junit:4. '
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

However when I run the app on my device "Samsung" the data does not load and the Oncancelled method is called when these rules are used:

{
  "rules": {
    ".read": false,
    ".write": false
  }
}

And when read and write are set to true the app just crashes:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

I am guessing that there is one thing I did wrong or am missing out, what is it? I am new to firebase so explaining as well as help would be appreciated, thankyou.

edit: Stack trace on logcat reads: 02-14 16:40:29.800 32134-32142/? W/SQLiteConnectionPool: A SQLiteConnection object for database ' data data com_android_vending databases resource_data_db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.

edit2: when the app crashes this error is given to me: 02-14 18:42:33.075 14095-14095/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main com.google.firebase.database.DatabaseException: Class com.example.myapplication.Dishes 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:570) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563) 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:203) at com.example.myapplication.MainActivity$1.onDataChange(MainActivity.java:54) 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:615) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4947) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) at dalvik.system.NativeStart.main(Native Method)

edit 3: I have solved the problem after doing snachsms answer, adding another empty constructor in dishes:

public Dishes(){}

    public Dishes(String name, float rating, int price) {
        this.name = name;
        this.price = price;
        this.rating = rating;
    }

Great thanks man and to everyone who contributed.

CodePudding user response:

Class com.example.myapplication.Dishes does not define a no-argument constructor.

so Dishes don't have no-argument constructor, thats correct as you have only one constructor in Dishes class

public Dishes(String name, float rating, int price) {

so you need yet another with no arguments at all, so

public Dishes() {}

public Dishes(String name, float rating, int price) {
   ... rest of code

and why this happens? maybe Firebase is using empty constructor for creating a Dishes object for some internal purposes, but thats guessing...

  • Related