Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "Default FirebaseApp is not initialized in this process" despite initializing FirebaseApp while using FirebaseStorage

I am trying to download images using Firebase Storage. I have written the following code

MainActivity.java

public class MainActivity extends AppCompatActivity {
ArrayList<Car> carListSuper;
RecyclerView rvCarList;
ProgressBar progressbar;
private CarListAdapter carListAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    carListSuper = new ArrayList<>();
    Firebase myFirebaseRef = new Firebase("https://carbar-7dae7.firebaseio.com/");

    rvCarList = (RecyclerView) findViewById(R.id.rvCarList);
    rvCarList.setItemAnimator(new FeedItemAnimator());
    progressbar = (ProgressBar) findViewById(R.id.progressCarLoad);
    carListAdapter = new CarListAdapter(carListSuper,MainActivity.this);

    RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
    rvCarList.setLayoutManager(linearLayoutManager);
    rvCarList.setAdapter(carListAdapter);

    myFirebaseRef.child("carList").addValueEventListener(new ValueEventListener() {

    @Override
    public void onDataChange(DataSnapshot snapshot) {

        Log.i("saz",snapshot.getValue().toString());
        progressbar.setVisibility(View.GONE);
        carListSuper.clear();
        //String jsonString = snapshot.getValue().toString();
        try {

            for (int i=0; i<snapshot.getChildrenCount();i++) {
                //Car carList = gson.fromJson(snapshot.child(i+""), Car.class);
                DataSnapshot snap = snapshot.child(i+"");
                carListSuper.add(FirebaseParser.carParser(snap));
            }
            carListAdapter.updatelist(carListSuper);
            Log.i("saz",snapshot.getValue().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override public void onCancelled(FirebaseError error) { }
});
}

CarListAdapter.java

public class CarListAdapter<T> extends RecyclerView.Adapter<CarListAdapter.ViewHolder> {

static int i = 0;
private static Context m_context;
List<Car> carList = null;
public static final int VIEW_TYPE_DEFAULT = 1;
public static final int VIEW_TYPE_LOADER = 2;

// Provide a suitable constructor (depends on the kind of dataset)
public CarListAdapter(List<Car> carList, Context context) {
    Log.i("saz", "dealerCircularCalls: " + this.carList);
    this.carList = carList;
    m_context = context;
}

// Create new views (invoked by the layout manager)
@Override
public CarListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.car_item, parent, false);
    // set the view's size, margins, paddings and layout parameters

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

public void updatelist(ArrayList<Car> list) {
    ArrayList tempList = new ArrayList();
    tempList.addAll(list);
    Log.i("saz", "Size on notifyupdate: " + list.size());
    carList.clear();
    carList.addAll(tempList);
    notifyDataSetChanged();
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    Log.i("saz", "position rv: " + position);
    /*if (position % 2 != 0) {
        holder.cv.setCardBackgroundColor(m_context.getResources().getColor(R.color.lighter_gray));
    } else {
        holder.cv.setCardBackgroundColor(m_context.getResources().getColor(R.color.white));
    }*/
    /*CarBarDb db = new CarBarDb(m_context,
            CarBarDb.DATABASE_NAME, null, CarBarDb.DATABASE_VERSION);*/
    Car car = carList.get(position);
    holder.provider.setText("Zoomcar");
    holder.carType.setText(car.getCarType());
    holder.carName.setText(car.getCarName());
    holder.tarrif.setText(car.getPrice_per_hour_weekday());



    if (m_context!=null) {
        FirebaseStorage storage = FirebaseStorage.getInstance(FirebaseApp.initializeApp(m_context));

        StorageReference storageRef = storage.getReferenceFromUrl("gs://carbar-7dae7.appspot.com/");

        storageRef.child("CarImages/figo.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                Picasso.with(m_context).load(uri)
                        .error(R.drawable.no_image)
                        .into(holder.imCarImage);
            }
        });
    }


    //db.close();
}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    Log.i("saz", "carList.size(): " + carList.size());
    return carList.size();
}


public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    CardView cv;
    TextView provider;
    TextView carName;
    TextView carType;
    TextView tarrif;
    ImageButton imCarImage;

    public ViewHolder(final View itemView) {
        super(itemView);

        cv = (CardView) itemView.findViewById(R.id.cvCarItem);
        provider = (TextView) cv.findViewById(R.id.tvProvider);
        carName = (TextView) cv.findViewById(R.id.tvCarName);
        carType = (TextView) cv.findViewById(R.id.tvCarType);
        tarrif = (TextView) cv.findViewById(R.id.tvTarrif);
        imCarImage = (ImageButton) cv.findViewById(R.id.imgCarImage);
    }
}

Still, I am getting the following error.

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.saz.firebasetest. Make sure to call FirebaseApp.initializeApp(Context) first.

And in main activity, I initialize this adapter as

carListAdapter = new CarListAdapter(carListSuper,MainActivity.this);

I searched on this forum but did not get what is the exact issue. Please help. Log lines:

FATAL EXCEPTION: main
Process: com.saz.firebasetest, PID: 6991
java.lang.IllegalArgumentException: Null is not a valid value for the FirebaseApp.
at com.google.android.gms.common.internal.zzac.zzb(Unknown Source)
at com.google.firebase.storage.FirebaseStorage.getInstance(Unknown Source)
at com.saz.firebasetest.controller.CarListAdapter.onBindViewHolder(CarListAdapter.java:86)
at com.saz.firebasetest.controller.CarListAdapter.onBindViewHolder(CarListAdapter.java:29)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
at android.view.View.layout(View.java:16975)
at android.view.ViewGroup.layout(ViewGroup.java:5579)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:16975)
at android.view.ViewGroup.layout(ViewGroup.java:5579)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:16975)
at android.view.ViewGroup.layout(ViewGroup.java:5579)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:433)
at android.view.View.layout(View.java:16975)
at android.view.ViewGroup.layout(ViewGroup.java:5579)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:16975)
at android.view.ViewGroup.layout(ViewGroup.java:5579)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1753)
at android.view.View.layout(View.java:16975)
at android.view.ViewGroup.layout(ViewGroup.java:5579)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2755)
at android.view.View.layout(View.java:16975)
at android.view.ViewGroup.layout(ViewGroup.java:5579)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2552)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2255)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1321)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6737)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
at android.view.Choreographer.doCallbacks(Choreographer.java:696)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.
--------- beginning of system
like image 438
Sarfaraz Avatar asked Sep 29 '16 06:09

Sarfaraz


2 Answers

You're mixing Firebase SDK versions.

This comes from a Firebase 2.x SDK:

Firebase myFirebaseRef = new Firebase("https://carbar-7dae7.firebaseio.com/");

But Firebase Storage is only included in Firebase 9.x.

You'll need to switch to a single Firebase SDK version. Given that you want to use Firebase Storage, that will be version 9.x.

If you follow the instructions on firebase.google.com, you can see the dependencies for the Database and Storage are:

dependencies {
  // ...
  compile 'com.google.firebase:firebase-database:9.6.1'
  compile 'com.google.firebase:firebase-storage:9.6.1'
}

Whether you use exactly 9.6.1 doesn't matter all that much, but the versions must be the same, while you now likely include the database via compile 'com.firebase:firebase-client-android:2.5.2+'.

like image 147
Frank van Puffelen Avatar answered Sep 25 '22 02:09

Frank van Puffelen


Seems you are doing the following:

carListAdapter = new CarListAdapter(carListSuper,MainActivity.this);

That should be the following: In your activity

Firebase.setAndroidContext(this);
Firebase myFirebaseRef = new Firebase("https://carbar-7dae7.firebaseio.com/");
carListAdapter = new CarListAdapter(carListSuper,this);

and as a suggestion I would not do

FirebaseStorage storage = FirebaseStorage.getInstance(FirebaseApp.initializeApp(m_context));
    StorageReference storageRef = storage.getReferenceFromUrl("gs://carbar-7dae7.appspot.com/");

in the onBindViewHolder method. Would do this in the constructor and make it a class variable.

like image 31
QVDev Avatar answered Sep 23 '22 02:09

QVDev