当前位置: 首页 > 知识库问答 >
问题:

尽管在使用FirebaseStorage时初始化了FirebaseApp,但仍获得“默认FirebaseApp未在此进程中初始化”

汤修贤
2023-03-14

我正在尝试使用Firebase存储下载图像。我编写了以下代码

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

共有1个答案

牛昱
2023-03-14
Firebase myFirebaseRef = new Firebase("https://carbar-7dae7.firebaseio.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+'包含数据库。

 类似资料:
  • 我创建了一个新的项目,并通过firebase的android studio工具添加了firebase。按照指示做了一切,但我在午餐时得到了这个错误的应用程序。 MainActivity.java是 而gradle.build是 项目级分级。Build 在网上找不到任何东西!已添加,这在任何说明中都没有,但在模拟器和设备上仍然存在相同的错误。

  • 甚至我也调用了FirebaseApp.InitializeApp(this);在很多地方 MyApplication(扩展应用程序),在活动的onCreate中调用

  • 我正在开发一个Android程序,使用Firebase数据库,但我现在有问题。 这是我的代码主要活动:

  • 我在Visual Studio2019中有一个Xamarin表单解决方案。我现在只使用Android版本。完整的错误是: 在此进程中,java.lang.IllegalStateException Message=Default FirebaseApp未初始化AppCardView.Sample.android。确保首先调用FirebaseApp.InitializeApp(上下文)。 在我的An

  • 我在使用FirebaseUI时遇到了麻烦。当我运行我的应用程序时,出现以下问题:“默认FirebaseApp在此进程中未初始化。请确保首先调用FirebaseApp.InitializeApp(上下文)。” 在我的应用程序/构建中。Gradle我有依赖: 、 、 、  我已经将GoogleServices.json导出到“app”文件夹。我别无选择,问题还在继续。有什么建议吗? logcat: -

  • 我开始为那句话发疯了: IllegalStateException:默认FirebaseApp未在此进程中初始化com.talkabouttv.talkaboutTV。确保首先调用FirebaseApp.InitializeApp(上下文)。 我不知道是什么引起的。我正在调用firebaseapp.intapp(this),我的包名匹配...为什么它不能工作? }