Problems creating Child in Firebase

0

I'm trying to add dishes in my firebase database but I'm having some problems that I do not know until now why it does not work. and even gives this error:

Process: meals.com.meals, PID: 1585
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
   at com.google.android.gms.internal.zg.zzar(Unknown Source)
   at com.google.android.gms.internal.zh.zzas(Unknown Source)
   at com.google.android.gms.internal.zg.zzao(Unknown Source)
at com.google.android.gms.in

For you to see what I am doing I will also post the activity code and the dish class.

public class CriarPrato extends AppCompatActivity {

private Spinner NomeRestaurante;
private EditText NomePrato1;
private EditText PrecoPrato1;
private Button GerarPrato;
private String IDrestaurante;

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


    NomeRestaurante = (Spinner) findViewById(R.id.spinnerNomeRestaurantePrato);
    NomePrato1 = (EditText) findViewById(R.id.editTextNomePrato);
    PrecoPrato1 = (EditText) findViewById(R.id.editTextPrecoPrato);
    GerarPrato = (Button) findViewById(R.id.buttonPrato);

    Query queryPrato = ConfiguracaoFirebase.getReferencia().child("Restaurante").orderByChild("localRestaurante");

    queryPrato.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //  final List<String> localRest = new ArrayList<String>();
            final List<String> NomeRestauranteList = new ArrayList<String>();

            for (DataSnapshot data : dataSnapshot.getChildren()) {
                Restaurante restaurante = data.getValue(Restaurante.class);
                //   localRest.add(restaurante.getLocalRestaurante().toString());
                NomeRestauranteList.add(restaurante.getNome().toString());
            }

            //LocalRestaurante = (Spinner) findViewById(R.id.SpinerLocalPrato);
            // ArrayAdapter<String> areas = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item, localRest);
            // areas.setDropDownViewResource(R.layout.spinner_item);
            // LocalRestaurante.setAdapter(areas);

            NomeRestaurante = (Spinner) findViewById(R.id.spinnerNomeRestaurantePrato);
            ArrayAdapter<String> NomeRestAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.spinner_item, NomeRestauranteList);
            NomeRestAdapter.setDropDownViewResource(R.layout.spinner_item);
            NomeRestaurante.setAdapter(NomeRestAdapter);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    Query query = ConfiguracaoFirebase.getReferencia().child("Restaurante").orderByChild("nome").equalTo("McDonald's");

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot data : dataSnapshot.getChildren()) {
                IDrestaurante = data.getKey();
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    GerarPrato.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Prato prato = new Prato();
            prato.setID(IDrestaurante);
            prato.setNomePrato(NomePrato1.getText().toString());
            prato.setPrecoPrato(PrecoPrato1.getText().toString());

            prato.Salvar(IDrestaurante);
        }
    });
}

Note: I know I'm not using the spinner, I had some problems getting the value so I put the value in the hand, to do some tests.

public class Prato {

    private String ID;
    private String NomePrato;
    private String PrecoPrato;

    public Prato() {
    }

    public void Salvar(String key){
        DatabaseReference reference = ConfiguracaoFirebase.getReferencia();
        reference.child("Restaurante").child(key).child("Cardapio").child(getID()).setValue(this);
    }
    @Exclude
    public String getID() {
        return ID;
    }

    public void setID(String key) {
        reference = ConfiguracaoFirebase.getReferencia().child("Restaurante").child(key).child("Cardapio");
        this.ID = reference.push().getKey();
    }

    public String getNomePrato() {
        return NomePrato;
    }

    public void setNomePrato(String nomePrato) {
        NomePrato = nomePrato;
    }

    public String getPrecoPrato() {
        return PrecoPrato;
    }

    public void setPrecoPrato(String precoPrato) {
        PrecoPrato = precoPrato;
    }

    public DatabaseReference getReference() {
        return reference;
    }

    public void setReference(DatabaseReference reference) {
        this.reference = reference;
    }

    private DatabaseReference reference;

}

Follows an image of the database organization for better understanding

I want to add in each restaurant a node called Menu and inside this menu node I will put my products creating us With the id's and inside this is's going to have the variables: dish name and value in the dish.

    
asked by anonymous 11.10.2017 / 16:14

1 answer

0

I do not know if I understood correctly but from what I realized of its history, I would do the bank with this structure: ....notsavingfromyourmodelclasswouldlooklikethis:

publicvoidSalvar(Stringkey){DatabaseReferencereference=ConfiguracaoFirebase.getReferencia();reference.child("Restaurante")
          .child("Iguatemi")
          .child("cardapio")
          .child(nomeLoja)
          .push()
          .child(prato.getNomeRestaurante)//opcional, assim vc teria o nome do bobs seguido de nós com o nome do prato e o valor
          .setValue(this)
}

This, thinking that Iguatemi is a mall and has some stores in it, if it is a restaurant you could delete the .child (nameLoja), and to return the data:

Query queryPrato = ConfiguracaoFirebase.getReferencia().child("Restaurante").child(nomeRestaurante);

I think it's easiest to get the nodes in the valueEventListener.

    
12.10.2017 / 15:25