0

I have an activity (say A) which is launched in singleTask mode. I created a popup in this activity and added it to the parent view of activity A. Now I launched Activity A from somewhere else and I receive onNewIntent() call, where I reinitialize the new view. But how do I destroy this popup before I reinitialize A?

Here is my code : This is how I created popup.

    final ViewGroup rootView = (ViewGroup) getWindow().getDecorView().getRootView();
        RelativeLayout popup = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.popup_r,null);
popupView = popup;
rootView.addView(popup);

Here is my onNewIntent method

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        if (popupView != null)
        {
            ((ViewGroup)popupView.getParent()).removeView(popupView);
            popupView = null;
        }        

        setIntent(intent);
        setContentView(R.layout.activity_view);
        reinitView();
    }

But this gives me crash, here is the logcat:

java.lang.NullPointerException: Attempt to write to field 'android.view.ViewParent android.view.View.mParent' on a null object reference

at line ((ViewGroup)popupView.getParent()).removeView(popupView);

How do I remove popupview before reinitializing activity with new intent?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Pardeep Kumar
  • 900
  • 1
  • 14
  • 30
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – AskNilesh Mar 01 '18 at 04:52
  • What you are doing is removing a view which was created in some other instance, that is why it is giving null pointer. – iamgopal Mar 01 '18 at 04:59
  • @iamgopal i also think so. But how do i remove it ? – Pardeep Kumar Mar 01 '18 at 05:08

0 Answers0