您无法在尚未连接的View或getActivity()返回null的Fragment上开始加载 [英] You cannot start a load on a not yet attached View or a Fragment where getActivity() returns null

查看:133
本文介绍了您无法在尚未连接的View或getActivity()返回null的Fragment上开始加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FragmentStatePagerAdapter在一个活动中显示大约5个片段.在每个活动中,我正在显示从FirebaseListAdapter/FirebaseRecyclerAdapter中获取的图像.由于是FragmentStagePagerAdapter,相邻视图/片段即使不可见也将被初始化.如果选项卡以快速方式滚动(前进/后退),应用程序崩溃并出现上述错误.我在互联网上发现了一些类似的问题,但没有能够解决此问题.如果我开始新的活动(在活动中,我会在网格视图中显示来自Firebase的图像),并且我还收到无法为破坏的活动启动加载错误立即(按返回按​​钮) 我正在使用以下代码.

I am using FragmentStatePagerAdapter to show around 5 fragments in an activity.On each activity I am showing the images which I am fetching from FirebaseListAdapter/FirebaseRecyclerAdapter. As it is FragmentStagePagerAdapter adjacent views/Fragments will be initialized even they are not visible.If the tabs are scrolled in a fast manner (forward/backward) App is crashing with above mentioned error.I found out some similar issues on the internet but not able to fix this issue.I am also getting You cannot start a load for a destroyed activity error if I am starting a new activity(In activity I am showing images from firebase in a grid view) and closing it immediately(pressing the back button) I am using following code.

adapt= new FirebaseListAdapter<MyClassStudent>(getActivity(), MyClassStudent.class, R.layout.mychild_grid_template, myRef) {
            @Override
            protected void populateView(final View v, MyClassStudent model, int position) {
                final TextView uname = (TextView) v.findViewById(R.id.mychild_uname);

                final CircleImageView profileImage=(CircleImageView)v.findViewById(R.id.mychild_image);
                studRef.child(model.getUsername()).addListenerForSingleValueEvent(new ValueEventListener() {
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        Child child=dataSnapshot.getValue(Child.class);
                        uname.setText(child.getUsername());
                        Glide.with(getActivity()).load(child.getProfileImage()).into(profileImage);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        };

堆栈跟踪:

E/UncaughtException: java.lang.NullPointerException: You cannot start a load on a not yet attached View or a  Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).
                         at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:27)
                         at com.bumptech.glide.Glide.getRetriever(Glide.java:509)
                         at com.bumptech.glide.Glide.with(Glide.java:563)
                         at com.mycompany.educareteacher.FragmentStudents$2$1.onDataChange(FragmentStudents.java:184)
                         at com.google.firebase.database.zzp.onDataChange(Unknown Source)
                         at com.google.android.gms.internal.tl.zza(Unknown Source)
                         at com.google.android.gms.internal.vg.zzHW(Unknown Source)
                         at com.google.android.gms.internal.vm.run(Unknown Source)
                         at android.os.Handler.handleCallback(Handler.java:739)
                         at android.os.Handler.dispatchMessage(Handler.java:95)
                         at android.os.Looper.loop(Looper.java:148)
                         at android.app.ActivityThread.main(ActivityThread.java:5438)
                         at java.lang.reflect.Method.invoke(Native Method)
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
D/FA: Logging event (FE): app_exception(_ae), Bundle[{firebase_event_origin(_o)=crash, firebase_screen_class(_sc)=ClassDetailActivity, firebase_screen_id(_si)=-4663411025690523798, timestamp=1500710706898, fatal=1}]
V/FA: Recording user engagement, ms: 3544
D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=3544, firebase_screen_class(_sc)=ClassDetailActivity, firebase_screen_id(_si)=-4663411025690523798}]
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.mycompany.com.educareteacher, PID: 5154
                  java.lang.NullPointerException: You cannot start a load on a not yet attached View or a  Fragment where getActivity() returns null (which usually occurs when getActivity() is called before the Fragment is attached or after the Fragment is destroyed).
                      at com.bumptech.glide.util.Preconditions.checkNotNull(Preconditions.java:27)
                      at com.bumptech.glide.Glide.getRetriever(Glide.java:509)
                      at com.bumptech.glide.Glide.with(Glide.java:563)
                      at com.mycompany.educareteacher.FragmentStudents$2$1.onDataChange(FragmentStudents.java:184)
                      at com.google.firebase.database.zzp.onDataChange(Unknown Source)
                      at com.google.android.gms.internal.tl.zza(Unknown Source)
                      at com.google.android.gms.internal.vg.zzHW(Unknown Source)
                      at com.google.android.gms.internal.vm.run(Unknown Source)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5438)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

我该如何解决这个问题?

How Can I solve this issue?

推荐答案

您的片段在Firebase响应之前已分离,因此请尝试在使用getActivity() null之前将其检查

Your fragment is detached before response coming from firebase, So try to check getActivity() null before using that

public void onDataChange(DataSnapshot dataSnapshot) {
    if (getActivity() == null) {
        return;
    }
    Child child = dataSnapshot.getValue(Child.class);
    uname.setText(child.getUsername());
    Glide.with(getActivity()).load(child.getProfileImage()).into(profileImage);
}

您可以检查活动是否为null或不在其他地方.

You can do like to check activity is null or not in other place.

private Activity mActivity;

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    mActivity = getActivity();
}

@Override
public void onDetach() {
    super.onDetach();
    mActivity = null;
}

private void doAction() {
    if (mActivity == null) {
        return;
    }

    adapt = new FirebaseListAdapter<MyClassStudent>(mActivity, MyClassStudent.class, R.layout.mychild_grid_template, myRef) {
        @Override
        protected void populateView(final View v, MyClassStudent model, int position) {
            final TextView uname = (TextView) v.findViewById(R.id.mychild_uname);

            final CircleImageView profileImage = (CircleImageView) v.findViewById(R.id.mychild_image);
            studRef.child(model.getUsername()).addListenerForSingleValueEvent(new ValueEventListener() {
                public void onDataChange(DataSnapshot dataSnapshot) {
                    if (mActivity == null) {
                        return;
                    }
                    Child child = dataSnapshot.getValue(Child.class);
                    uname.setText(child.getUsername());
                    Glide.with(mActivity).load(child.getProfileImage()).into(profileImage);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    };
}

这篇关于您无法在尚未连接的View或getActivity()返回null的Fragment上开始加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆