无法解析符号 'MobileAds'
Cannot resolve symbol 'MobileAds'
好的,所以我正在开发一个 android 应用程序,我试图在其中放置广告,但每当我尝试编译它时都会收到错误 "Cannot resolve symbol 'MobileAds'"。
这是我拥有的:
activity_main.java
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(getApplicationContext(), "ca-app-pub-Removed");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
我的XML:
xmlns:ads="http://schemas.android.com/apk/res-auto"
<com.google.android.gms.ads.AdView
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="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
和
<string name="banner_ad_unit_id">ca-app-pub-(removed)</string>
项目Gradle
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
应用Gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-ads:9.6.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
为什么我会收到此错误消息?
在此方面的任何帮助表示赞赏。谢谢!
对于遇到此问题的任何人:
确保你有
import com.google.android.gms.ads.MobileAds;
在你的MainActivity.java
如果您在 activity 中有导入,那么请确保在 Gradle 项目级别中有 Maven:
maven {
url "https://maven.google.com"
}
首先确保您已经完成了 Google AdMob 入门的所有步骤,
https://developers.google.com/admob/android/quick-start
然后转到 Android Studio > 工具 > SDK 管理器 > SDK 工具
并检查您是否已安装
Google 播放服务和 Google 存储库enter image description here
如果没有安装它然后去
Android 工作室 > 文件 > 与 gradle 文件同步项目
好的,所以我正在开发一个 android 应用程序,我试图在其中放置广告,但每当我尝试编译它时都会收到错误 "Cannot resolve symbol 'MobileAds'"。
这是我拥有的:
activity_main.java
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(getApplicationContext(), "ca-app-pub-Removed");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
我的XML:
xmlns:ads="http://schemas.android.com/apk/res-auto"
<com.google.android.gms.ads.AdView
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="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
和
<string name="banner_ad_unit_id">ca-app-pub-(removed)</string>
项目Gradle
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
应用Gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-ads:9.6.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
为什么我会收到此错误消息? 在此方面的任何帮助表示赞赏。谢谢!
对于遇到此问题的任何人:
确保你有
import com.google.android.gms.ads.MobileAds;
在你的MainActivity.java
如果您在 activity 中有导入,那么请确保在 Gradle 项目级别中有 Maven:
maven {
url "https://maven.google.com"
}
首先确保您已经完成了 Google AdMob 入门的所有步骤,
https://developers.google.com/admob/android/quick-start
然后转到 Android Studio > 工具 > SDK 管理器 > SDK 工具 并检查您是否已安装 Google 播放服务和 Google 存储库enter image description here
如果没有安装它然后去 Android 工作室 > 文件 > 与 gradle 文件同步项目