以下类不能被实例化:com.google.ads.AdView()
我正在天气app.My代码工作正常,但有广告不showing.Below是我的代码:以下类不能被实例化:com.google.ads.AdView()
activity_main。 XML
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:loadAdOnCreate="true"
app:adSize="BANNER"
app:adUnitId="......" >
</com.google.ads.AdView>
的AndroidManifest.xml:
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
MainActivity.java
import com.google.ads.AdRequest;
import com.google.ads.AdView;
.
.
.
private AdView adview;
.
.
AdRequest adRequest = new AdRequest();
if(adview!=null)
{
adview.loadAd(adRequest);
}
错误(类无法实例化:com.google.ads.AdView)以activity_main.xml的图形布局显示。搜索了很多问题,但找不到合适的答案。请帮忙。
我发现,我是用旧/过时的方法。为了添加Admob,你现在不能使用eclipse。解决的方法是(在不使用火力):
中的build.gradle添加依赖(buildscript后):
buildscript{
.
.
}
dependencies{
compile 'com.google.android.gms:play-services:6.5.87'
}
添加代码MainActivity.java
adview = (AdView) findViewById(R.id.ad);
AdRequest adRequest = new AdRequest.Builder().build();
adview.loadAd(adRequest);
代码添加到activity_main.xml中
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/ad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="@string/ad_id">
</com.google.android.gms.ads.AdView>
添加到AndroidManifest.xml中
标签之外<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
附加代码在AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Google更新了firebase-ads中的新内容。
在你gradle这个文件中添加com.google.firebase:火力的广告
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.firebase:firebase-ads:9.8.0'
}
我在eclipse中制作,所以如何添加依赖关系。对不起,为什么倒投了。 –
参考http://stackoverflow.com/questions/25123447/admob-new-adrequest-unrecognized-by-eclipse – sasikumar
我正在做同样的答案。我并没有将旧的/弃用的admob API与新的admob API混合在一起。我只用过老式的admob api。 –
需要导入GoogleAdMobAdsSdk-X.X.X.jar文件到您的项目。 – Swathin
我已经导入GoogleAdMobAdsSdk-6.4.1.jar文件。 –