-1

I am trying to create an incident reporting app where it is connected to firebase and whenever I create an incident in the application it will show up in the Firebase Database as well as in the form of Cardview and RecyclerView in the app itself. My app can already show the output in Firebase and now I am trying to get the CardView and RecyclerView done.

However, when I run my application now, whenever I try to click on the fragment that allows me to input incidents, the application crashes and displays this error:

java.lang.NoSuchMethodError: No virtual method zzbox()Z in class Lcom/google/firebase/FirebaseApp; or its super classes (declaration of 'com.google.firebase.FirebaseApp' appears in /data/app/com.example.samuel.a4-CppiMKcbuMktQgH7RUO1XA==/split_lib_dependencies_apk.apk)

The supposed error is this line in my fragment:

mDatabase = FirebaseDatabase.getInstance().getReference().child("Incidents");

Keep in mind this is in a fragment and the tutorial that I watched was in a normal class.

Here is the fragment from where I am inputting my incidents into

package com.example.samuel.a4;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.github.clans.fab.FloatingActionButton;
import com.github.clans.fab.FloatingActionMenu;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import org.w3c.dom.Text;


public class IncmanFragment extends Fragment {

private RecyclerView mBlogList;
private DatabaseReference mDatabase;

FloatingActionButton fab;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
ViewGroup container, @Nullable Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.fragment_incman, container, 
false);
     //i moved this up from just ontop of fab to here
    View myView = inflater.inflate(R.layout.fragment_incman, container, 
false);
   // getFragmentManager().findFragmentById(R.id.fragment_container);
    mDatabase = 
FirebaseDatabase.getInstance().getReference().child("Incidents");
    mDatabase.keepSynced(true);

    mBlogList=(RecyclerView) myView.findViewById(R.id.recyclerview);
    mBlogList.setHasFixedSize(true);
    mBlogList.setLayoutManager(new LinearLayoutManager(getActivity()));


    fab = (FloatingActionButton) myView.findViewById(R.id.fabIncident);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentTransaction fragmentTransaction = 
getFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, new 
ReportFragment());
            fragmentTransaction.addToBackStack(null);
          /*  Toast.makeText(getActivity(), "toast successful", 
Toast.LENGTH_SHORT).show();*/
            fragmentTransaction.commit();

        }

    });

    return myView;
}

@Override
public void onStart() {
    super.onStart();
    FirebaseRecyclerAdapter<IncmanFirebaseName, IncmanViewHolder> 
    firebaseRecyclerAdapter = new 
    FirebaseRecyclerAdapter<IncmanFirebaseName, 
    IncmanViewHolder>
            (IncmanFirebaseName.class, R.layout.incidentdesign, 
      IncmanViewHolder.class, mDatabase) {
        @Override
        protected void populateViewHolder(IncmanViewHolder viewHolder, 

            viewHolder.setTitle(model.getTitle());
            viewHolder.setDetails(model.getDetailsId());
            viewHolder.setRemarks(model.getRemarksId());
            viewHolder.setLocation(model.getLocationId());
            viewHolder.setDate(model.getDate());
            viewHolder.setUrgency(model.getUrgencyId());

        }
    };

    mBlogList.setAdapter(firebaseRecyclerAdapter);

}

    public static class IncmanViewHolder extends RecyclerView.ViewHolder
    {
        View mView;
        public IncmanViewHolder(View itemView)
        {
            super(itemView);
            mView = itemView;
        }
        public void setTitle(String title)
        {
            TextView tvtitledesign = 
    (TextView)mView.findViewById(R.id.tvtitledesign);
            tvtitledesign.setText(title);
        }
        public void setDetails(String detailsId)
        {
            TextView tvdetailsdesign = 
    (TextView)mView.findViewById(R.id.tvdetailsdesign);
            tvdetailsdesign.setText(detailsId);
        }
        public void setRemarks(String remarksId)
        {
            TextView tvremarksdesign = 
    (TextView)mView.findViewById(R.id.tvremarksdesign);
            tvremarksdesign.setText(remarksId);
        }
        public void setLocation(String locationId)
        {
            TextView tvlocationdesign = (TextView) 
    mView.findViewById(R.id.tvlocationdesign);
            tvlocationdesign.setText(locationId);

        }
        public void setDate (String date)
        {
            TextView tvdatedesign = 
     (TextView)mView.findViewById(R.id.tvdatedesign);
            tvdatedesign.setText(date);
        }
        public void setUrgency(String urgencyId)
        {
            TextView tvurgencydesign = 
     (TextView)mView.findViewById(R.id.tvurgencydesign);
            tvurgencydesign.setText(urgencyId);
        }
    }
}

This is the class where the constructor and get and set methods are

 package com.example.samuel.a4;

    public class IncmanFirebaseName {

    private String date;
    private String detailsId;
    private String locationId;
    private String remarksId;
    private String reportedById;
    private String splocationId;
    private String typeId;
    private String urgencyId;
    private String title;

    public IncmanFirebaseName(String date, String detailsId, String 
    remarksId/*String reportedById*/ /* String typeId*/, String urgencyId, 
    String 
    title) {
        this.date = date;
        this.detailsId = detailsId;
      /*  this.locationId = locationId;*/
        this.remarksId = remarksId;
        /*this.reportedById = reportedById;*/
        this.splocationId = splocationId;
        /*this.typeId = typeId;*/
        this.urgencyId = urgencyId;
        this.title=title;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDetailsId() {
        return detailsId;
    }

    public void setDetailsId(String detailsId) {
        this.detailsId = detailsId;
    }

    public String getLocationId() {
        return locationId;
    }

    public void setLocationId(String locationId) {
        this.locationId = locationId;
    }

    public String getRemarksId() {
        return remarksId;
    }

    public void setRemarksId(String remarksId) {
        this.remarksId = remarksId;
    }

    /*public String getReportedById() {
        return reportedById;
    }

    public void setReportedById(String reportedById) {
        this.reportedById = reportedById;
    }*/

    /* public String getSplocationId() {
        return splocationId;
    }

    public void setSplocationId(String splocationId) {
        this.splocationId = splocationId;
    }*/

     /*   public String getTypeId() {
        return typeId;
    }

    public void setTypeId(String typeId) {
        this.typeId = typeId;
    }*/

    public String getUrgencyId() {
        return urgencyId;
    }

    public void setUrgencyId(String urgencyId) {
        this.urgencyId = urgencyId;
    }

    public String getTitle(){
        return title;
    }
    public void setTitle() {
        this.title = title;
    }
    public IncmanFirebaseName()
    {

    }
    }

Database Structure

Now I am not sure why the logcat shows that line as an error in my code because I have a similar line in another fragment and it is working perfectly fine; here is the fragment: " package com.example.samuel.a4;

    import android.app.DatePickerDialog;
    import android.app.ProgressDialog;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.Color;
    import android.graphics.drawable.ColorDrawable;
    import android.media.Image;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.text.TextUtils;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.DatePicker;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.Spinner;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.google.android.gms.tasks.OnCompleteListener;
    import com.google.android.gms.tasks.OnFailureListener;
    import com.google.android.gms.tasks.OnSuccessListener;
    import com.google.android.gms.tasks.Task;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.storage.FirebaseStorage;
    import com.google.firebase.storage.OnProgressListener;
    import com.google.firebase.storage.StorageReference;
    import com.google.firebase.storage.UploadTask;

    import org.w3c.dom.Text;

    import java.io.IOException;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.UUID;

    import static android.app.Activity.RESULT_OK;

    public class ReportFragment extends Fragment implements 
    AdapterView.OnItemSelectedListener{

    private Button btnChoose,btnUpload;
    private ImageView imageView;
    private Uri filePath;
    private final int PICK_IMAGE_REQUEST = 71;
    private TextView mDisplayDate;
    private DatePickerDialog.OnDateSetListener mDateSetListener;

    private EditText reportedBy;
    private TextView date; //upstairs declare alr as mDisplayDate
    private Spinner spinner; //downstairs declare alr as spinner3
    private EditText location;
    private Spinner spinner2; //downstairs declare alr as spinner2
    private EditText details;
    private Spinner spinner3; //downstairs declare alr as spinner1
    private EditText remarks;
    private EditText title;
    private Button submitIncident;

    DatabaseReference databaseIncidents;
    FirebaseStorage storage;
    StorageReference storageReference;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable 
    ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_report, container, 
false);

          databaseIncidents = 
    FirebaseDatabase.getInstance().getReference("Incidents");
        storage = FirebaseStorage.getInstance();
        storageReference = storage.getReference();

        btnChoose = (Button) v.findViewById(R.id.btnChoose);
        btnUpload = (Button) v.findViewById(R.id.btnUpload);
        imageView = (ImageView) v.findViewById(R.id.imgView);

        reportedBy = (EditText) v.findViewById(R.id.etreportedby);
    //        Below have already line 151
    //        date = (TextView) v.findViewById(R.id.tvdate);
        location = (EditText) v.findViewById(R.id.etlocation);
        details = (EditText) v.findViewById(R.id.etdetails);
        remarks = (EditText) v.findViewById(R.id.etremarks);
        title = (EditText) v.findViewById(R.id.ettitle);
        submitIncident = (Button) v.findViewById(R.id.btnSubmit);


        btnChoose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseImage();
            }
        });
        btnUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadImage();
            }
        });

        submitIncident.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                incidentSubmit();
            }
        });

        return v;


    }

    private void incidentSubmit(){
        String reportedname = reportedBy.getText().toString().trim();
        String location1 = location.getText().toString();
        String details1 = details.getText().toString();
        String remarks1 = remarks.getText().toString();
        String urgency1 = spinner.getSelectedItem().toString();
        String type1 = spinner2.getSelectedItem().toString();
        String splocation1 = spinner3.getSelectedItem().toString();
        String date1 = mDisplayDate.getText().toString();
        String title1 = title.getText().toString();

        if(!TextUtils.isEmpty(reportedname)) {

            String incidentId = databaseIncidents.push().getKey();
            Incident incident = new 

  Incident(reportedname,location1,details1,remarks1,urgency1,type1,splocation1,date1,title1);
            databaseIncidents.child(incidentId).setValue(incident);

            Toast.makeText(getActivity(), "Incident Added", 
    Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getActivity(), "All fields must be 
    entered",Toast.LENGTH_LONG).show();
        }

    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, 
    long id) {
        String text = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    }

Any help is sincerely appreciated. Thank you!

code
  • 29
  • 1
  • 7

1 Answers1

0

Okay, so from your comment and from seeing your code, I think you're confusing between setting the reference to your database and actually retrieving the values.

The code line below, just sets the reference to Incidents child in your Firebase Database.

mDatabase = FirebaseDatabase.getInstance().getReference().child("Incidents");

To retrieve the values from your database, you have to use eventListeners, Read more about eventListeners in this answer.

I am giving you a sample code to retrieve the value of locationId node of your database for the first unique id. It looks something like this:

mDatabase.child("LR9j...").addListenerForSingleValueEvent(new ValueEventListener() {
// in the child("LR9j..") you have to put the whole first id from your image
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String locId = dataSnapshot.child("locationId").getValue(String.class);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Log.d(TAG, "onCancelled", databaseError.toException());
            }


        });

This above code will store value of first unique Id's locationId in the variable locId. Also you have to understand that Firebase retrieves values asynchronously, so it will only work from inside the eventListener.

Read more about Firebase asynchronous behaviour in this answer.

Also read more about Firebase in the docs.

PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
  • Hi, actually for the id, it is automatically generated with every new incident that I add from my application. So I would like to ask if there is any alternative to that first line "mDatabase.child("LR9j...").addListenerForSingleValueEvent(new ValueEventListerner()" because if I were to create a new incident would it mean I have to continue writing more of these codes? and if I were to delete the incidents would it mean I would have to delete the codes too? – code Nov 19 '18 at 05:57
  • For this precise reason, there is `push()` and `getKey()`. Read more about that here https://firebase.google.com/docs/database/admin/save-data#getting-the-unique-key-generated-by-push, also do mark the answer as correct by clicking on V type tick mark button next to the answer, this helps future readers of the question and I'd appreciate that too. Cheers! :) – PradyumanDixit Nov 19 '18 at 06:10
  • I just tried adding the sample, it still showed me the same error! I am not sure if I placed the sample code in the correct area but I placed it in the Fragment titled "Incman Fragment" (First code area) right above "return myView". Did I place wrongly, or am I supposed to fill up all the other inputs first? Or there is something else wrong? I really appreciate your help! – code Nov 19 '18 at 06:24
  • Hi, after researching more, I found out it could be due to gradle issues and I somewhat got my application to work now! Thank you very much! – code Nov 19 '18 at 07:04