我正在尝试使用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) { }
});
}
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
答案 0 :(得分:4)
您正在混合Firebase SDK版本。
这来自Firebase 2.x SDK:
Firebase myFirebaseRef = new Firebase("https://carbar-7dae7.firebaseio.com/");
但Firebase存储仅包含在Firebase 9.x中。
您需要切换到单个Firebase SDK版本。鉴于您要使用Firebase存储,这将是版本9.x。
如果您遵循instructions on 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+'
包含数据库。
答案 1 :(得分:1)
似乎您正在执行以下操作:
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/");
作为建议我不会做
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let rootVC = storyboard.instantiateViewControllerWithIdentifier("ViewControllerID") as! UIViewController
self.window!.rootViewController = rootVC
在onBindViewHolder方法中。将在构造函数中执行此操作并使其成为类变量。
答案 2 :(得分:0)
请在适配器的构造函数中添加以下行,以便在bindviewholder方法中初始化它。
FirebaseStorage storage = FirebaseStorage.getInstance(FirebaseApp.initializeApp(m_context));