javaandroidfacebook-ads

App crashes after trying to add a banner Ad at the navigation header section


So i've added a linearlayout for my banner ad container in my nav_header.xml file . I called it out in my MainActivity.java file using the LinearLayout class . The app crashes when i try to launch it and gives the following exception :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference

Pointing to this line in my Main java file :

  // ADS section
    mAdView = new AdView(this, "IMG_16_9_APP_INSTALL#YOUR_PLACEMENT_ID", AdSize.BANNER_HEIGHT_90);
    LinearLayout adContainer = (LinearLayout) findViewById(R.id.banner_container);
    adContainer.addView(mAdView);
    mAdView.loadAd();

I tried using a Layout Infalter to the adContainer but it failed. Here is my nav_header.xml file :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="192dp"
    android:background="@color/c1"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:orientation="vertical"
    android:gravity="bottom">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/banner_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        />
</RelativeLayout>
</LinearLayout>

Here is my activity main file :

    <?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:id="@+id/drawerLayout"
    android:layout_height="match_parent">


    <include layout="@layout/container_layout"/>

    <com.google.android.material.navigation.NavigationView

        android:id="@+id/nav_view"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        android:elevation="10dp"
        app:menu="@menu/drawer_view"
        tools:targetApi="lollipop" />



</androidx.drawerlayout.widget.DrawerLayout>

Solution

  • NullPointerException occurs because banner_container isn't a part of the activity_main XML. It is a part of the nav_header XML, which isn't readily accessed by the activity hierarchy.

    To access the navigation header:

    View headerView = ((NavigationView)findViewById(R.id.nav_view)).getHeaderView(0);
    LinearLayout container = (LinearLayout) headerView.findViewById(R.id.banner_container);
    

    I hope this helps!!

    Edit: Casted NavigationView