我试图使用Firebase Storage
和Picasso
在ImageView中加载图像,但它在logcat中显示空指针异常。请问我错在哪里?下面是我的MainActiviy.class
package oerrride.we.huzykamz.imageretrieval;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.squareup.picasso.Picasso;
import oerrride.we.huzykamz.testingappfirebase.R;
public class MainActivity extends AppCompatActivity {
public FirebaseStorage mStorage;
ImageView img;
StorageReference storageRef = mStorage.getReferenceFromUrl("gs://askdoctor-546bf.appspot.com");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStorage= FirebaseStorage.getInstance();
img =(ImageView) findViewById(R.id.img);
StorageReference spaceRef = storageRef.child("askdoctor/namasuba.jpg");
String url = spaceRef.getBucket();
System.out.println("Here is the print of url : "+url);
Picasso.with(MainActivity.this).load(url)
.error(R.mipmap.ic_launcher).
resize(50, 50)
.placeholder(R.mipmap.ic_launcher).into(img);
}
}
那么这是我的logcat错误。
09-24 21:05:50.767 17655-17655/oerrride.we.huzykamz.testingappfirebase I/art: Not late-enabling -Xcheck:jni (already on)
09-24 21:05:50.799 17655-17655/oerrride.we.huzykamz.testingappfirebase I/art: Late-enabling JIT
09-24 21:05:50.844 17655-17655/oerrride.we.huzykamz.testingappfirebase I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
09-24 21:05:50.946 17655-17655/oerrride.we.huzykamz.testingappfirebase W/System: ClassLoader referenced unknown path: /data/app/oerrride.we.huzykamz.testingappfirebase-1/lib/x86
09-24 21:05:51.566 17655-17669/oerrride.we.huzykamz.testingappfirebase W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
09-24 21:05:51.789 17655-17662/oerrride.we.huzykamz.testingappfirebase W/art: Suspending all threads took: 53.648ms
09-24 21:05:51.827 17655-17669/oerrride.we.huzykamz.testingappfirebase W/GooglePlayServicesUtil: Google Play services out of date. Requires 9452000 but found 8087470
09-24 21:05:51.829 17655-17655/oerrride.we.huzykamz.testingappfirebase W/GooglePlayServicesUtil: Google Play services out of date. Requires 9452000 but found 8087470
09-24 21:05:51.830 17655-17655/oerrride.we.huzykamz.testingappfirebase I/DynamiteModule: Considering local module com.google.android.gms.flags:1 and remote module com.google.android.gms.flags:0
09-24 21:05:51.830 17655-17655/oerrride.we.huzykamz.testingappfirebase I/DynamiteModule: Selected local version of com.google.android.gms.flags
09-24 21:05:51.850 17655-17655/oerrride.we.huzykamz.testingappfirebase W/DynamiteModule: Local module descriptor class for com.google.android.gms.crash not found.
09-24 21:05:51.852 17655-17655/oerrride.we.huzykamz.testingappfirebase W/GooglePlayServicesUtil: Google Play services out of date. Requires 9452000 but found 8087470
09-24 21:05:51.852 17655-17655/oerrride.we.huzykamz.testingappfirebase I/DynamiteModule: Considering local module com.google.android.gms.crash:0 and remote module com.google.android.gms.crash:0
09-24 21:05:51.853 17655-17655/oerrride.we.huzykamz.testingappfirebase E/FirebaseCrash: Failed to initialize crash reporting
com.google.firebase.crash.internal.zzg$zza: com.google.android.gms.internal.zzsb$zza: No acceptable module found. Local version is 0 and remote version is 0.
at com.google.firebase.crash.internal.zzg.zzbq(Unknown Source)
at com.google.firebase.crash.FirebaseCrash.<init>(Unknown Source)
at com.google.firebase.crash.FirebaseCrash.getInstance(Unknown Source)
at java.lang.reflect.Method.invoke(Native Method)
at com.google.firebase.FirebaseApp.zza(Unknown Source)
at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
at com.google.firebase.FirebaseApp.zzek(Unknown Source)
at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)
at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: com.google.android.gms.internal.zzsb$zza: No acceptable module found. Local version is 0 and remote version is 0.
at com.google.android.gms.internal.zzsb.zza(Unknown Source)
at com.google.firebase.crash.internal.zzg.zzbq(Unknown Source)
at com.google.firebase.crash.FirebaseCrash.<init>(Unknown Source)
at com.google.firebase.crash.FirebaseCrash.getInstance(Unknown Source)
at java.lang.reflect.Method.invoke(Native Method)
at com.google.firebase.FirebaseApp.zza(Unknown Source)
at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
at com.google.firebase.FirebaseApp.zzek(Unknown Source)
at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)
at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
at android.app.ActivityThread.installProvider(ActivityThread.java:5153)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-24 21:05:51.896 17655-17655/oerrride.we.huzykamz.testingappfirebase I/FA: App measurement is starting up, version: 9452
09-24 21:05:51.896 17655-17655/oerrride.we.huzykamz.testingappfirebase I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
09-24 21:05:51.964 17655-17655/oerrride.we.huzykamz.testingappfirebase I/FirebaseInitProvider: FirebaseApp initialization successful
09-24 21:05:52.028 17655-17686/oerrride.we.huzykamz.testingappfirebase W/GooglePlayServicesUtil: Google Play services out of date. Requires 9452000 but found 8087470
09-24 21:05:52.142 17655-17690/oerrride.we.huzykamz.testingappfirebase W/GooglePlayServicesUtil: Google Play services out of date. Requires 9452000 but found 8087470
09-24 21:05:52.143 17655-17690/oerrride.we.huzykamz.testingappfirebase W/DynamiteModule: Local module descriptor class for com.google.android.gms.firebasestorage not found.
09-24 21:05:52.143 17655-17690/oerrride.we.huzykamz.testingappfirebase I/DynamiteModule: Considering local module com.google.android.gms.firebasestorage:0 and remote module com.google.android.gms.firebasestorage:0
09-24 21:05:52.143 17655-17690/oerrride.we.huzykamz.testingappfirebase E/NetworkRqFactoryProxy: NetworkRequestFactoryProxy failed with a RemoteException:
com.google.android.gms.internal.zzsb$zza: No acceptable module found. Local version is 0 and remote version is 0.
at com.google.android.gms.internal.zzsb.zza(Unknown Source)
at com.google.android.gms.internal.zzami.<init>(Unknown Source)
at com.google.android.gms.internal.zzami.zzi(Unknown Source)
at com.google.firebase.storage.StorageReference.zzcxz(Unknown Source)
at com.google.firebase.storage.zzb.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
09-24 21:05:52.143 17655-17690/oerrride.we.huzykamz.testingappfirebase E/GetMetadataTask: Unable to create firebase storage network request.
android.os.RemoteException
at com.google.android.gms.internal.zzami.<init>(Unknown Source)
at com.google.android.gms.internal.zzami.zzi(Unknown Source)
at com.google.firebase.storage.StorageReference.zzcxz(Unknown Source)
at com.google.firebase.storage.zzb.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
09-24 21:05:52.144 17655-17690/oerrride.we.huzykamz.testingappfirebase E/StorageException: StorageException has occurred.
An unknown error occurred, please check the HTTP result code and inner exception for server response.
Code: -13000 HttpResult: 0
09-24 21:05:52.144 17655-17690/oerrride.we.huzykamz.testingappfirebase E/StorageException: null
android.os.RemoteException
at com.google.android.gms.internal.zzami.<init>(Unknown Source)
at com.google.android.gms.internal.zzami.zzi(Unknown Source)
at com.google.firebase.storage.StorageReference.zzcxz(Unknown Source)
at com.google.firebase.storage.zzb.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
09-24 21:05:52.172 17655-17666/oerrride.we.huzykamz.testingappfirebase I/art: Background sticky concurrent mark sweep GC freed 11792(944KB) AllocSpace objects, 5(100KB) LOS objects, 66% free, 1226KB/3MB, paused 6.481ms total 46.096ms
09-24 21:05:52.397 17655-17698/oerrride.we.huzykamz.testingappfirebase D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
09-24 21:05:52.481 17655-17655/oerrride.we.huzykamz.testingappfirebase W/FA: Service connection failed: ConnectionResult{statusCode=SERVICE_VERSION_UPDATE_REQUIRED, resolution=null, message=null}
09-24 21:05:52.579 17655-17698/oerrride.we.huzykamz.testingappfirebase I/OpenGLRenderer: Initialized EGL, version 1.4
09-24 21:05:52.747 17655-17698/oerrride.we.huzykamz.testingappfirebase W/EGL_emulation: eglSurfaceAttrib not implemented
09-24 21:05:52.747 17655-17698/oerrride.we.huzykamz.testingappfirebase W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabe30560, error=EGL_SUCCESS
09-24 21:05:52.899 17655-17662/oerrride.we.huzykamz.testingappfirebase W/art: Suspending all threads took: 6.593ms
09-24 21:06:02.284 17655-17686/oerrride.we.huzykamz.testingappfirebase I/FA: Tag Manager is not found and thus will not be used
09-24 21:06:02.307 17655-17686/oerrride.we.huzykamz.testingappfirebase W/GooglePlayServicesUtil: Google Play services out of date. Requires 9452000 but found 8087470
09-24 21:06:02.310 17655-17655/oerrride.we.huzykamz.testingappfirebase W/FA: Service connection failed: ConnectionResult{statusCode=SERVICE_VERSION_UPDATE_REQUIRED, resolution=null, message=null}
09-24 21:06:09.863 17655-17662/oerrride.we.huzykamz.testingappfirebase W/art: Suspending all threads took: 5.043ms
在Activity对象初始化期间执行以下语句时,mStorage
尚未初始化并且为null:
StorageReference storageRef = mStorage.getReferenceFromUrl("gs://askdoctor-546bf.appspot.com");
修改storageRef
的声明以移除初始化式:
StorageReference storageRef;
然后在onCreate()
中初始化:
mStorage= FirebaseStorage.getInstance();
mStorageRef = mStorage.getReferenceFromUrl("gs://askdoctor-546bf.appspot.com");