web-dev-qa-db-ja.com

FirebaseStorageの使用中にFirebaseAppを初期化したにもかかわらず、「デフォルトのFirebaseAppはこのプロセスで初期化されません」を取得する

FirebaseStorageを使用して画像をダウンロードしようとしています。私は次のコードを書きました

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);
    }
}

それでも、次のエラーが発生します。

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

そしてメインアクティビティでは、このアダプタを次のように初期化します

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

このフォーラムで検索しましたが、正確な問題がわかりませんでした。助けてください。ログライン:

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
5
Sarfaraz

FirebaseSDKのバージョンを混在させています。

これはFirebase2.xSDKからのものです。

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

ただし、FirebaseStorageはFirebase9.xにのみ含まれています。

単一のFirebaseSDKバージョンに切り替える必要があります。 Firebase Storageを使用する場合、バージョン9.xになります。

firebase.google.comの手順 に従うと、データベースとストレージの依存関係が次のようになります。

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

正確に9.6.1を使用するかどうかはそれほど重要ではありませんが、バージョンは同じである必要がありますが、compile 'com.firebase:firebase-client-Android:2.5.2+'を介してデータベースを含める可能性があります。

4

あなたは次のことをしているようです:

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

それは次のようになります:あなたの活動で

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

そして提案として私はしません

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

onBindViewHolderメソッドで。コンストラクターでこれを行い、クラス変数にします。

1
QVDev

アダプターのコンストラクターに以下の行を追加して、bindviewholderメソッド内ではなく初期化してください。

 FirebaseStorage storage = FirebaseStorage.getInstance(FirebaseApp.initializeApp(m_context));
0
GrIsHu