在片段内获取Glide的上下文会导致错误

时间:2016-10-11 14:36:36

标签: java android android-fragments

在这种方法中,我试图使用框架从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)

2 个答案:

答案 0 :(得分:1)

对不起,如果我迟到了,但我有一些帮助我的事情,这个案子..所以它可能是愚蠢的,我不知道为什么它有效,所以放手吧..  我暗示了一个背景

context=getactivity().getapplicationContext();

和Glide方法

 Glide.with(context.getApplicationContext())
            .load(Url).asBitmap().fitCenter()
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {

                  //your stuff here


                }
            });

答案 1 :(得分:0)

看起来您正在尝试使用mBmap,但get()意味着要在后台线程中使用,因此mBmap仍为null。方法into(int width, int height)也已弃用。加载到Bitmap的{​​{3}}如下:

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