添加横幅广告,findViewById 给出 "not enough information to infer type variable."

Adding banner advert, findViewById giving "not enough information to infer type variable."

所以我的第一个应用程序运行良好,我正在尝试添加横幅广告并按照演练进行操作。

我的主要 activity xml 只放了一个 nav_host_fragment 所以我把横幅 xml 放在那个片段中。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.main.ButtonFragment">

    <ImageView
        android:id="@+id/lightSwitch"
        android:layout_width="258dp"
        android:layout_height="527dp"
        android:background="?android:attr/colorBackground"
        android:contentDescription="@string/light_switch"
        android:src="@drawable/ic_light_on"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintBottom_toBottomOf="parent">
    </com.google.android.gms.ads.AdView>

</androidx.constraintlayout.widget.ConstraintLayout>

根据漫游主要activity需要加载广告

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)

        MobileAds.initialize(this) {}
        
        mAdView = findViewById(R.id.adView)
        val adRequest = AdRequest.Builder().build()
        mAdView.loadAd(adRequest)
        
    }

但是我在 findviewById 上收到“没有足够的信息来推断类型变量”的错误。我有点卡住了,不胜感激。

我认为问题在于横幅视图是在片段的 xml 中定义的,但您试图在 activity 中找到该视图,因此 android studio 未检测到类型自动。

假设您在 activity 的 xml 中定义和显示片段,并且您的 main_activity 看起来像这样

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

   <fragment
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/nav_host_fragment "
       android:name="com.example.myapplication.BlankFragment"/>

</androidx.constraintlayout.widget.ConstraintLayout>

您需要明确说明视图的类型,因此请更新 mAdView = findViewById(R.id.adView)mAdView = findViewById<AdView>(R.id.adView)

记得在activity中导入AdView