0

I am facing an issue when I am trying to move from fragment to activity but it's showing below stack trace. Why getActivity() not getting reference in Fragment.And the main thing is next activity is called but when I am taking intent action on next activity to another activity then it's showing this error.

07-25 15:54:33.150 8662-8662/io.funswitch.gymmon E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.funswitch.gymmon, PID: 8662
    java.lang.IllegalStateException: Fragment CheckInFragment{1fa737a} not attached to Activity
        at android.support.v4.app.Fragment.startActivity(Fragment.java:1000)
        at android.support.v4.app.Fragment.startActivity(Fragment.java:991)
        at io.funswitch.gymmon.Fragments.CheckInFragment$1$1.onCallback(CheckInFragment.java:175)
        at io.funswitch.gymmon.Fragments.CheckInFragment$6.onDataChange(CheckInFragment.java:507)
        at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source)
        at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6123)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

I am attaching MainActvity by below lines previously when I am not attaching an activity to this fragment then it is showing getActivity() == null.

public MainActivity activity;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.activity = (MainActivity) context;

    }

Below is my code for Intent. Here I am changing from fragment to activity. I also registered this activity in maniest too.

readData(new MyCallback() {
                    @Override
                    public void onCallback(double value, double lng, String gymName) {
                        if (distance(value, lng, location.getLatitude(), location.getLongitude()) <= 0.1) {
                            LocalDate localDate = new LocalDate();
                            String date1 = localDate.toString();
                            DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
                            databaseReference.child("checkInStatus").setValue("1");
                            databaseReference.child("checInDate").setValue(date1);
                            databaseReference.keepSynced(true);
                            Intent intent = new Intent(activity, CheckOutActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                            startActivity(intent);
                        } else {
                            final Snackbar snackbar = Snackbar
                                    .make(coordinatorLayout, getString(R.string.not_at_gym_location), Snackbar.LENGTH_LONG)
                                    .setAction("DISMISS", new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {

                                        }
                                    });
                            snackbar.setActionTextColor(Color.BLACK);
                            // Changing action button text color
                            View sbView = snackbar.getView();
                            sbView.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.orange_bt));
                            TextView textView = sbView.findViewById(android.support.design.R.id.snackbar_text);
                            textView.setTextColor(Color.WHITE);

                            snackbar.show();
                        }
                    }
                });

Please help me out. I am not getting how to resolve this I searched on other posts but not helpful.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

0 Answers0