-2

` FATAL EXCEPTION: main Process: com.intracity, PID: 9569 android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@99f7d19 is not valid; is your activity running? t android.view.ViewRootImpl.setView(ViewRootImpl.java:808) enter code here at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:351) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) at android.widget.Toast$TN.handleShow(Toast.java:465) at android.widget.Toast$TN$2.handleMessage(Toast.java:347) at android.os.Handler.dispatchMessage(Handler.java:110) at android.os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6339) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1084) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:945) '

Activity Code: this method i used to fetch realtime firebase location from node after giving 20 to 40 updates it got crash.Thanks!!

'

public void DataData() {
    try {
        FirebaseOptions options = new FirebaseOptions.Builder()
                .setApplicationId("1:673426911334:android:31ed7fa834e0c6f4") // Required for Analytics.
                .setApiKey("AIzaSyD8o7uS21z1ZeN17cTdIjx6EQBV3lgJoQs") // Required for Auth.
                .setDatabaseUrl("https://intracitydriver.firebaseio.com/") // Required for RTDB.
                .build();
        FirebaseApp.initializeApp(this, options, "sec");
        FirebaseApp secondary = FirebaseApp.getInstance("sec");
        FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(secondary);

        secondaryDatabase.getReference("9728824163").addValueEventListener(new com.google.firebase.database.ValueEventListener() {
            @Override
            public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
                for (com.google.firebase.database.DataSnapshot locationSnapshot : dataSnapshot.getChildren()) {`enter code here`
                    String location = locationSnapshot.getValue().toString();
                    String[] strings = location.split("=");
                    String aDouble = (strings[2]);
                    String aDouble2 = (strings[3]);
                    String[] newlat = aDouble.split(",");
                    String[] newlon = aDouble2.split("\\}");
                    Double lat = Double.valueOf(newlat[0]);
                    Double lon = Double.valueOf(newlon[0]);
                   //               String s=strings[5];
                    Toast.makeText(Home.this, ""+lat+lon, Toast.LENGTH_SHORT).show();




                    Log.d("Locations updated", "location: " + location);
                    Log.e("Yogesh", lat + "hello" + lon);
                    final LatLng latLng = new LatLng(lat, lon);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            // NEWTruckMarker(latLng);
                        }
                    }, 1000);


                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    } catch (Exception e) {'
      //  Toast.makeText(this, "" + e, Toast.LENGTH_SHORT).show();
        e.printStackTrace();
        Log.e("Crash Found: ",""+e);'

    }

}

'

Hemant Parmar
  • 3,552
  • 7
  • 22
  • 45
yogesh saluja
  • 21
  • 1
  • 7

1 Answers1

-1

The reason for this exception is that, as the exception message says, the activity has finished but you are trying to access the Context of the finished activity. So you need to check for Activiy's Existence on callback. See the code below for reference .

secondaryDatabase.getReference("9728824163").addValueEventListener(new com.google.firebase.database.ValueEventListener() {
    @Override
    public void onDataChange (com.google.firebase.database.DataSnapshot dataSnapshot){
        if(!isFinishing()){
            // Do your stuff here
        }

    }
}

NOTE- And also you have to remove ValueEventListener when your Activity is Destroyed. Probably in onStop() or in onDestroy().

ADM
  • 16,256
  • 11
  • 37
  • 69
  • but my activity is still in process.I am using this data to show in activity.If activity got finished then how can i make sure why my activity got finished. – yogesh saluja Jan 19 '18 at 06:07
  • You don't need to check if the activity is finishing before using its context otherwise it would add needless boilerplate code everywhere. The use case for `isFinshing` is in my experience to avoid starting long running tasks that would be discarded because the activity would be dead before they return. In this scenario, just unsubscribing from Firebase event listener would suffice in `onStop`. – Puneet Jan 19 '18 at 06:07
  • ok bt how can i make use this data as long time process like if i am doing realtime tracking. – yogesh saluja Jan 19 '18 at 06:10
  • If you are doing realtime tracking then your your activity must be in forground right? So this is what i have mentioned Register listener in `onResume` and remove it in `onPause()`. – ADM Jan 19 '18 at 06:13
  • yes my activity is in foreground.Ok let me try this – yogesh saluja Jan 19 '18 at 06:22