我从firebase实时数据库中获取图像时遇到问题。图像存储在存储中,我在数据库中有一个链接,但是当我尝试将图像显示在recyclerview中时,应用程序在尝试加载记录时崩溃。我在gradle.app中添加了依赖项,并且已经使用了Internet
获取数据的代码
@Override
public void onBindViewHolder(RestaurantViewHolder holder, int position) {
Restaurant restaurant = restaurantList.get(position);
holder.title.setText(restaurant.getTitle());
holder.address.setText(restaurant.getAddress());
holder.cuisine.setText(restaurant.getCuisine());
holder.duration.setText(restaurant.getDuration());
holder.PriceRange.setText(restaurant.getPriceRange());
Picasso.with(context).load(restaurantList.get(position).getImage()).resize(120,90).into(holder.imageView);
}
设置数据的代码
public class RestaurantViewHolder extends RecyclerView.ViewHolder{
TextView title, address, cuisine, duration, PriceRange;
ImageView imageView;
public RestaurantViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.textViewTitle);
address = itemView.findViewById(R.id.textViewAddress);
cuisine = itemView.findViewById(R.id.textViewCuisine);
duration = itemView.findViewById(R.id.textViewDuration);
PriceRange = itemView.findViewById(R.id.textViewPriceRange);
imageView = itemView.findViewById(R.id.viewImage);
}
}
设置/获取方法
public class Restraunt{
public Restaurant(String image) {
this.image = image;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
崩溃日志
04-20 20:30:14.528 30759-30759/nci.wherenow E/AndroidRuntime: FATAL EXCEPTION: main
Process: nci.wherenow, PID: 30759
java.lang.IllegalArgumentException: Context must not be null.
at com.squareup.picasso.Picasso$Builder.<init>(Picasso.java:701)
at com.squareup.picasso.Picasso.with(Picasso.java:662)
at nci.wherenow.RestaurantAdapter.onBindViewHolder(RestaurantAdapter.java:48)
at nci.wherenow.RestaurantAdapter.onBindViewHolder(RestaurantAdapter.java:19)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3109)
at android.view.View.measure(View.java:22104)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:22104)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6584)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:22104)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6584)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
at android.view.View.measure(View.java:22104)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6584)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22104)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6584)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22104)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6584)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:722)
at android.view.View.measure(View.java:22104)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2441)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1522)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1775)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1410)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6834)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:778)
at android.view.Choreographer.doFrame(Choreographer.java:713)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
更新了崩溃日志:
04-20 20:37:12.153 855-855/nci.wherenow E/AndroidRuntime: FATAL EXCEPTION: main
Process: nci.wherenow, PID: 855
com.google.firebase.database.DatabaseException: Class nci.wherenow.Restaurant does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
at com.google.android.gms.internal.zzelx.zze(Unknown Source:45)
at com.google.android.gms.internal.zzelw.zzb(Unknown Source:198)
at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at nci.wherenow.Restaurant_Results$1.onChildAdded(Restaurant_Results.java:58)
at com.google.android.gms.internal.zzecw.zza(Unknown Source:71)
at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
at com.google.android.gms.internal.zzeig.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
04-20 20:37:12.154 855-855/nci.wherenow E/UncaughtException: com.google.firebase.database.DatabaseException: Class nci.wherenow.Restaurant does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
at com.google.android.gms.internal.zzelx.zze(Unknown Source:45)
at com.google.android.gms.internal.zzelw.zzb(Unknown Source:198)
at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
at nci.wherenow.Restaurant_Results$1.onChildAdded(Restaurant_Results.java:58)
at com.google.android.gms.internal.zzecw.zza(Unknown Source:71)
at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
at com.google.android.gms.internal.zzeig.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
崩溃更新2
04-20 20:45:41.030 4227-4227/nci.wherenow E/AndroidRuntime: FATAL EXCEPTION: main
Process: nci.wherenow, PID: 4227
java.lang.IllegalArgumentException: Context must not be null.
at com.squareup.picasso.Picasso$Builder.<init>(Picasso.java:701)
at com.squareup.picasso.Picasso.with(Picasso.java:662)
at nci.wherenow.RestaurantAdapter.onBindViewHolder(RestaurantAdapter.java:48)
at nci.wherenow.RestaurantAdapter.onBindViewHolder(RestaurantAdapter.java:19)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3109)
at android.view.View.measure(View.java:22104)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:22104)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6584)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:22104)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6584)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
at android.view.View.measure(View.java:22104)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6584)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22104)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6584)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22104)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6584)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:722)
at android.view.View.measure(View.java:22104)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2441)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1522)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1775)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1410)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6834)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
at android.view.Choreographer.doCallbacks(Choreographer.java:778)
at android.view.Choreographer.doFrame(Choreographer.java:713)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6809)
at java.lang.reflect.Method.invoke(Native Method)
04-20 20:45:41.031 4227-4227/nci.wherenow E/AndroidRuntime: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
答案 0 :(得分:0)
//在模型中声明你的餐厅空构造函数;
Restaurant.java
public Restaurant() {
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
主要课程: - 替换你的代码onBindViewHolder
Restaurant restaurant = new Restaurant();
Picasso.with(getContext()).load(restaurant.getImage()).resize(200, 200).into(holder.
imageView);
答案 1 :(得分:0)
在您的活动中试试这个,通过&#39;这个&#39;适配器如:
RecyclerAdapter r = new RecyclerAdapter(this);
然后在您的RecyclerAdapter类构造函数中:
Context context; // global
public RecyclerAdapter(Context context){
this.context = context;
}
答案 2 :(得分:0)
您的上下文为空,这是问题,尝试将上下文从活动(您将适配器添加到回收器视图的位置)传递到RestaurantAdapter(到构造函数)并将其与picasso一起使用。
在您的活动中使用getBaseContext()(您创建了Restaurent适配器的实例)并将其传递给适配器构造函数并在picasso中使用它。
答案 3 :(得分:0)
您只需要初始化上下文。您可以将它传递给适配器构造函数,如上所述。或者,您可以在Application类中定义静态上下文,并在Application类onCreate方法中初始化它。但第一个更有可能。