In Java Android Studio when I look at my code, I see the error: Cannot resolve symbol "mAdView" I checked on the AdMob website and it seems I did the code right but I get this error.
Here is my main java code:
package com.example.javaamogbutton;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button amogussus = findViewById(R.id.amogus);
final MediaPlayer mediaPlayer = MediaPlayer.create(this,R.raw.amongus);
amogussus.setOnClickListener(v -> mediaPlayer.start()); {
}
MobileAds.initialize(this, initializationStatus -> {
});
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
}
build.gradle:
buildscript {
repositories {
google()
mavenCentral()
}
}
plugins {
id com.android.application
}
android {
namespace com.example.javaamogbutton
compileSdk 33
defaultConfig {
applicationId "com.example.javaamogbutton"
minSdk 27
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile( proguard-android-optimize.txt ), proguard-rules.pro
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation androidx.appcompat:appcompat:1.6.1
implementation com.google.android.material:material:1.9.0
implementation androidx.constraintlayout:constraintlayout:2.1.4
implementation com.google.android.gms:play-services-ads:22.1.0
testImplementation junit:junit:4.13.2
androidTestImplementation androidx.test.ext:junit:1.1.5
androidTestImplementation androidx.test.espresso:espresso-core:3.5.1
}
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<Button
android:id="@+id/amogus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/amogussus"
android:textColor="@color/black"
android:layout_centerInParent="true"
android:textSize="24sp"/>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="MY_ID">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
Importing AdView and other imports didn t work. My program said import com.google.android.gms.ads.AdView;
was an unused import.