您不能在尚未附加的视图或 getActivity() 返回 null 的片段上开始加载

问题描述 投票:0回答:8

我正在使用 FragmentStatePagerAdapter 在一个活动中显示大约 5 个片段。在每个活动中,我都显示我从 FirebaseListAdapter/FirebaseRecyclerAdapter 获取的图像。因为它是 FragmentStagePagerAdapter 相邻的视图/片段将被初始化,即使它们不可见。如果选项卡以快速方式滚动(向前/向后)应用程序崩溃并出现上述错误。我在互联网上发现了一些类似的问题但没有能够解决这个问题。如果我开始一个新活动(在活动中,我在网格视图中显示来自 firebase 的图像)并立即关闭它(按返回键) 我正在使用以下代码。 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)

我该如何解决这个问题?

android android-fragments firebase android-glide
8个回答
10
投票
getActivity()

null


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

您可以在其他地方检查活动是否为空。

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



4
投票
isAdded()

方法。


2
投票

public class NoteAdapter extends FirestoreRecyclerAdapter<Note,NoteAdapter.NoteHolder> { Context context; public NoteAdapter(@NonNull FirestoreRecyclerOptions<Note> options) { super(options); } @Override protected void onBindViewHolder(@NonNull NoteHolder holder, int position, @NonNull final Note model) { context = holder.itemView.getContext(); holder.r_tv.setText(model.getTitle()); Glide.with(context).load(model.getImage()).into(holder.r_iv); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(context, CategoryProductActivity.class); MainActivity.myCategory2 = model.getTitle(); MainActivity.myCategoryIcon2 = model.getImage(); context.startActivity(i); } }); } @NonNull @Override public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_category,parent,false); return new NoteHolder(v); } public static class NoteHolder extends RecyclerView.ViewHolder { TextView r_tv; ImageView r_iv; public NoteHolder(@NonNull View itemView) { super(itemView); r_tv = itemView.findViewById(R.id.r_tv); r_iv = itemView.findViewById(R.id.r_iv); } } }



1
投票
    在存在 RecyclerView 的 Activity 上添加以下代码
  1. public static Context context;

  2. 然后在存在 RecyclerView 的同一 Activity 的
  3. onCreate

    中添加以下代码

    
    
    context = getApplicationContext();

  4. 然后参考glide如下图
  5. Glide.with(YourActivity.context).load("Image Link").into(holder.YourImageView);

    
        

0
投票

if (getActivity != null) { Glide.with(getActivity()).load(imageUrl).into(yourImageView); }



0
投票
当你错过在适配器的构造函数中添加上下文时就会出现这个问题,你可以很容易地解决它:-)

不要忘记在构造函数中添加 this.context = context

public groupUserAdapter(Context context, ArrayList<Users> groupUsers) { this.groupUsers = groupUsers; this.context = context; };



0
投票
onBindViewHolder

里面添加如下代码 context = holder.itemView.getContext();

例子

@Override protected void onBindViewHolder(@NonNull NoteHolder holder, int position, @NonNull final Note model) { context = holder.itemView.getContext(); Glide.with(context).load(model.getImage()).into(holder.r_iv); }



-1
投票
替换自

clientAdp = new ClientAdp(context,clientData);

clientAdp = new ClientAdp(getApplicationContext(),clientData);

就我而言

© www.soinside.com 2019 - 2024. All rights reserved.