【问题标题】:App crashes after trying to add a banner Ad at the navigation header section尝试在导航标题部分添加横幅广告后,应用程序崩溃
【发布时间】:2021-03-09 23:12:56
【问题描述】:

所以我在 nav_header.xml 文件中为横幅广告容器添加了线性布局。我使用 LinearLayout 类在 MainActivity.java 文件中调用它。当我尝试启动它时应用程序崩溃并给出以下异常:

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

在我的主 java 文件中指向这一行:

  // 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();

我尝试对 adContainer 使用 Layout Infalter,但失败了。 这是我的 nav_header.xml 文件:

    <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>

这是我的活动主文件:

    <?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>

【问题讨论】:

    标签: java android facebook-ads


    【解决方案1】:

    出现NullPointerException 是因为banner_container 不是activity_main XML 的一部分。它是nav_header XML 的一部分,活动层次结构不容易访问。

    要访问导航标题:

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

    我希望这会有所帮助!

    编辑:铸造的 NavigationView

    【讨论】:

    • 编辑答案,试试那个版本,可能是选角问题
    • 现在检查@agega
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    相关资源
    最近更新 更多