在这种方法中,我尝试使用框架从URL检索图像并将其存储为位图。但是,如果尝试使用它,则会出现nullpointer异常。

“ java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean android.app.Activity.isDestroyed()'”

我假设这与对Glide.with()中的上下文使用getActivity()有关。该方法(如下)在一个片段中。

private void generateMarker(final GeoLocation location, final GoogleMap globalMap, String userName, String key){
    DownloadImage downloadImage = new DownloadImage();

final String url = "https://graph.facebook.com/" + key + "/picture?type=large";

marker = new MarkerOptions().position(
        new LatLng(location.latitude, location.longitude)).title(userName);

//downloadImage.execute(url);
//downloadImage.setOnDownloadImageListener(this);

Bitmap mBmap = null;
try {
    mBmap = Glide.with(getActivity())
            .load(url)
            .asBitmap()
            .into(100, 100)
            .get();
} catch (Exception e){
    System.out.println(e);
}

//Doing something with mBmap that results in null


}

我的原木猫

 E/UncaughtException: java.lang.NullPointerException: null reference
                                                                at maps.w.d.a(Unknown Source)
                                                                at maps.ad.f$a.<init>(Unknown Source)
                                                                at maps.ad.f.a(Unknown Source)
                                                                at maps.ad.S.<init>(Unknown Source)
                                                                at maps.ad.t.a(Unknown Source)
                                                                at ua.onTransact(:com.google.android.gms.DynamiteModulesB:167)
                                                                at android.os.Binder.transact(Binder.java:387)
                                                                at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
                                                                at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
                                                                at com.test.project.PostFragment.generateMarker(PostFragment.java:255)
                                                                at com.test.project.PostFragment.access$300(PostFragment.java:51)
                                                                at com.test.project.PostFragment$3.onDataChange(PostFragment.java:224)
                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                at android.os.Looper.loop(Looper.java:158)
                                                                at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

最佳答案

看起来您正在尝试使用mBmap,但是get()是要在后台线程上使用的,因此mBmap仍然是null。方法into(int width, int height)也被弃用。要加载到Bitmap中的correct way将是以下内容:

Glide.with(getActivity())
    .load(url)
    .asBitmap()
    .into(new SimpleTarget<Bitmap>(100, 100) {
        @Override
        public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
            // Do something with bitmap here
        }
    };

关于java - 在 fragment 中获取Glide的上下文会导致错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39979804/

10-12 06:02