1

I am using Firebase database on my app, and sometimes when the data change the app crash:

Exception java.lang.IllegalStateException: Fragment Fragmento_Medios_FB{e0076f1} not attached to Activity
android.support.v4.app.Fragment.getResources (Fragment.java:646)
info.blacktrail.catedralapp.Fragmento_Medios_FB$2.onDataChange (Fragmento_Medios_FB.java:614)
com.google.android.gms.internal.zzbpx.zza ()
com.google.android.gms.internal.zzbqx.zzZT ()
com.google.android.gms.internal.zzbra$1.run ()
android.os.Handler.handleCallback (Handler.java:751)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:154)
android.app.ActivityThread.main (ActivityThread.java:6682)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)

I check everythings and nothing look wrong. any help?

This is my Fragment

public class Fragmento_Medios_FB extends Fragment {

    public Fragmento_Medios_FB() {
    }
    FragmentManager mFragmentManager;
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
    LinearLayout somelayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_medios_fb, container, false);
        somelayout=(LinearLayout)view.findviewById.(R.id.somelayout);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String someString = dataSnapshot.child("medios").getValue
                int Idsomeid = getResources().getIdentifier(edo_medios_sector_sur_01S, "drawable", getActivity().getPackageName());
                somelayout.setBackgroundResource(Idsomeid);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
}

The drawable is ok and also checked , and the database items, the crash occurs sometimes not every time the database change

nhoxbypass
  • 9,695
  • 11
  • 48
  • 71
Agustin Scalisi
  • 551
  • 3
  • 17

2 Answers2

2

Their are quite trick solution for this:

Activity activity = getActivity(); 
if(activity != null && isAdded()) {
   int Idsomeid = getResources().getIdentifier(edo_medios_sector_sur_01S, "drawable", getActivity().getPackageName());
}

This is happened clearly because your fragment are created but NOT YET attached to Activity when the data come from Firebase.

Many cases can return this exception like Fragment MyFragment not attached to Activity and java.lang.IllegalStateException: Fragment not attached to Activity

nhoxbypass
  • 9,695
  • 11
  • 48
  • 71
2

As exception says "Fragmento_Medios_FB not attached to Activity" it's mean that when your data changed and notify you by "addValueEventListener" Fragmento_Medios_FB wasn't attach. To prevent crashes you have to unregister listener in onStop method of lifecycle.

  • there is no onSTop method on this Fragment added – Agustin Scalisi Aug 17 '17 at 15:38
  • @AgustinScalisi so override it and fix your listener. To override method just start input onSt... in fragment body block and IDE will help you. The solution of GeniusQ is more trick then solution because onStart method calls much later after onAttach. – Eugene Troyanskii Aug 17 '17 at 15:52