Marco Di Scala
Marco Di Scala

Reputation: 484

"NullPointerException" getActivity().findViewById return null on Fragment in Android

I have a problem updating menu items of my Activity from a Fragment after an interstitial banner is closed (using "X" or clicking "back" it's the same). Before android HONEYCOMB everything works well also after interstitial banner, maybe because I use android.support.v4.app.Fragment.

in my activity with this layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/menuContainer"
        android:layout_width="match_parent"
        android:layout_height="32dp" />

    <FrameLayout
        android:id="@+id/fragments"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>

I add a new fragment and everyting goes fine:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragments, MyFragment.newInstance());
transaction.addToBackStack(null);
transaction.commit();

This is Fragment code:

public static MyFragment newInstance() {
    return new MyFragment();
}
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

private View mSimpleContainer;
private TextView mSimpleText;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_test, container, false);

    mSimpleContainer = view.findViewById(R.id.mSimpleContainer);
    mSimpleText = (TextView) view.findViewById(R.id.simpleText);

    return view;
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mSimpleText.setText("lorem ipsum");

    MyActivity act = (MyActivity) getActivity();

    if (act != null) {
        HorizontalScrollView menu = (HorizontalScrollView) act.findViewById(R.id.menuContainer);

        /**
        * menu is null and I get NullPointerException here after interstitial is closed.
        * Maybe the activity is paused and resumed but I expect that in onActivityCreated the activity is attached
        */
        menu.hideMenuItems();

        //This line open an Interstitial banner that when it's closed causes NullPointerException above
        act.showInterstitial(); //If I comment this line everything works fine
    }
}

When it's the correct time to call findViewById in the activity? Thanks for any answers

Upvotes: 0

Views: 1666

Answers (1)

Ratul Ghosh
Ratul Ghosh

Reputation: 1500

I think you r switching many fragment in the fragments FrameLayout. In that case the fragments are showing and hiding. So if any memory shortage occur fragmentManger is destroying invisible fragments view, but the are still attached to activity. so getActivity() is not returning null, but view is returning null. In this situation you will call getView(), if getView() return null then your fragments view is destroyed by fragmentManager , otherwise call getView().findViewById() it will return you valid view object. There is no problem in support.v4 fragment. May be FragmentManager of support.v4 are more memory concious.

Upvotes: 1

Related Questions