1

i'm a newbie when it comes to Android development i've been having an issue with my project which i can't seem to fix.

I have recently developed a food and recipe app the idea is to be able to upload recipes to FireBase, when i run the app i must choose an image from the gallery and type the name description and price, the problem is that after i uploaded the first recipe only the image was uploaded to my FireBase and the app immediately crashed as soon as i uploaded the recipe, now everytime i run the app it stops immediately and shows me a fatal exception, i've made this app watching Youtube videos from this channel https://www.youtube.com/channel/UCB2B0AuQgk6eOMbWR7qiqew I've commented on part 4 video about my issue since he's taking a long time to reply i'm seeking for help here, i don't know what went wrong and i hope someone can help me fix this.

Here's the error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.recipeapp, PID: 14845
    com.google.firebase.database.DatabaseException: Class com.example.recipeapp.FoodData 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(com.google.firebase:firebase-database@@16.0.4:557)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@16.0.4:550)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.0.4:420)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.0.4:214)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.0.4:79)
        at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.0.4:212)
        at com.example.recipeapp.MainActivity$1.onDataChange(MainActivity.java:69)
        at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
        at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
        at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

This is my MainActivity code:

package com.example.recipeapp;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

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

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;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    RecyclerView mRecyclerView;
    List<FoodData> myFoodList;
    FoodData mFoodData;
    ProgressDialog progressDialog;
    MyAdapter myAdapter;
    EditText txt_search;

    private DatabaseReference databaseReference;
    private ValueEventListener eventListener;

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

        mRecyclerView = findViewById(R.id.RecyclerView);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this, 1);
        mRecyclerView.setLayoutManager(gridLayoutManager);

        txt_search = (EditText) findViewById(R.id.txt_searchtext);

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Carregando Itens...");

        myFoodList = new ArrayList<>();

        final MyAdapter myAdapter = new MyAdapter(MainActivity.this, myFoodList);
        mRecyclerView.setAdapter(myAdapter);

        databaseReference = FirebaseDatabase.getInstance().getReference("Recipe");

        progressDialog.show();

        eventListener = databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                myFoodList.clear();

                for (DataSnapshot itemSnapshot : dataSnapshot.getChildren()) {

                    FoodData foodData = itemSnapshot.getValue(FoodData.class);
                    myFoodList.add(foodData);
                }

                myAdapter.notifyDataSetChanged();
                progressDialog.dismiss();

            }

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

                progressDialog.dismiss();

            }
        });

        txt_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {


            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                filter(s.toString());

            }
        });

    }

    private void filter(String text) {

        ArrayList<FoodData> filterList = new ArrayList<>();

        for (FoodData item : myFoodList) {

            if (item.getItemName().toLowerCase().contains(text.toLowerCase())) ;
            {

                filterList.add(item);

            }

        }

        myAdapter.filteredList(filterList);

    }


    public void btn_uploadActivity(View view) {

        startActivity(new Intent(this, Upload_Recipe.class));

    }
}

And here's my FoodData code:

package com.example.recipeapp;

public class FoodData {

    private String itemName;
    private String itemDescription;
    private String itemPrice;
    private String itemImage;

    public FoodData(String itemName, String itemDescription, String itemPrice, String itemImage) {
        this.itemName = itemName;
        this.itemDescription = itemDescription;
        this.itemPrice = itemPrice;
        this.itemImage = itemImage;
    }

    public String getItemName() {
        return itemName;
    }

    public String getItemDescription() {
        return itemDescription;
    }

    public String getItemPrice() {
        return itemPrice;
    }

    public String getItemImage() {
        return itemImage;
    }
}
Júlio Sordi
  • 31
  • 1
  • 5
  • I suggest doing web searches for any error message you don't understand. There is often people who get the same error. – Doug Stevenson Feb 28 '20 at 21:27
  • Thank you it's the first time i've seen this error and sometimes i have difficuties when searching but i'll see what i can dig up. – Júlio Sordi Feb 28 '20 at 21:34
  • I already did the search and marked the duplicates I found on Stack Oveflow. In the future, it's good to save your time and search first before asking. – Doug Stevenson Feb 28 '20 at 21:36
  • Sorry i did research it before but, no one matches my code with makes things harder, i don't have any experience i only practice at home through youtube. – Júlio Sordi Feb 28 '20 at 21:42
  • None of those solved my issue, the error doesn't even match, i don't even know what went wrong. – Júlio Sordi Feb 28 '20 at 21:56
  • The answers are all saying the same thing - add a constructor to your data object that doesn't take any arguments. If you aren't able to do this, then Firebase wont work with it the way you expect. – Doug Stevenson Feb 28 '20 at 21:59
  • You're missing is a `public FoodData(){}` constructor in your class. Both Alex and Rosário explain why in https://stackoverflow.com/a/48406247 – Frank van Puffelen Feb 28 '20 at 22:08
  • Thank you Frank i've set the missing constructor though it is not being used, sorry if i'm a little i have a few difficulties when searching for code. – Júlio Sordi Feb 29 '20 at 15:41

0 Answers0