Android google 广告错误(空引用?)
Android google ads error (null reference ?)
我实际上在过去的 24 小时里一直陷入这个错误,仍然找不到解决方案
错误描述:
11-06 18:53:37.745: E/AndroidRuntime(1698): Caused by:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)'
on a null object reference
这是一些 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_pattern"
android:orientation="vertical" >
.
.
.
.
.
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-xxxxxxx/xxxxxxxx" />
<TextView
android:id="@+id/tv_05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
mainActivity.java 中的 oncreate 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adview = (AdView) this.findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("")
.build();
adview.loadAd(adRequest);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(mainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(AppConstants.Interstitial_Id);
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
.....
我看不到任何错误我检查了所有导入库一切正常
请帮帮我,我 运行 没时间了:/
在XML中替换这个
xmlns:ads="http://schemas.android.com/apk/res-auto"
而不是
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
您需要将其放入 com.google.android.gms.ads.AdView 和 LinearLayout 中:
xmlns:ads="http://schemas.android.com/apk/res-auto"
编辑:
您需要从 LogCat 中提取测试设备的 ID 号。只需使用 AdManager 方法并将其设置为 TEST_EMULATOR,如 logcat 所说。如果您 运行 在带有 usb 调试的实际设备上观看 logcat,ID 将出现在其中。
它失败了,因为它没有在您的布局中找到 ID 为 adView
的元素。
确保上面显示的 XML 文件实际上是 activity_main.xml
.
并做一个干净的构建。
我遇到了类似的问题。当我仔细检查资源文件时,我的 activity:
有两个资源文件
\res\layout\activity_main_lauch.xml
\res\layout-v21\activity_main_lauch.xml
我正在修改单个文件,因此抛出错误。
当我在两个文件中应用更改时,它开始工作。
希望对您有所帮助。
我实际上在过去的 24 小时里一直陷入这个错误,仍然找不到解决方案 错误描述:
11-06 18:53:37.745: E/AndroidRuntime(1698): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
这是一些 xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_pattern"
android:orientation="vertical" >
.
.
.
.
.
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-xxxxxxx/xxxxxxxx" />
<TextView
android:id="@+id/tv_05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
mainActivity.java 中的 oncreate 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adview = (AdView) this.findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("")
.build();
adview.loadAd(adRequest);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(mainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(AppConstants.Interstitial_Id);
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
.....
我看不到任何错误我检查了所有导入库一切正常 请帮帮我,我 运行 没时间了:/
在XML中替换这个
xmlns:ads="http://schemas.android.com/apk/res-auto"
而不是
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
您需要将其放入 com.google.android.gms.ads.AdView 和 LinearLayout 中:
xmlns:ads="http://schemas.android.com/apk/res-auto"
编辑: 您需要从 LogCat 中提取测试设备的 ID 号。只需使用 AdManager 方法并将其设置为 TEST_EMULATOR,如 logcat 所说。如果您 运行 在带有 usb 调试的实际设备上观看 logcat,ID 将出现在其中。
它失败了,因为它没有在您的布局中找到 ID 为 adView
的元素。
确保上面显示的 XML 文件实际上是 activity_main.xml
.
并做一个干净的构建。
我遇到了类似的问题。当我仔细检查资源文件时,我的 activity:
有两个资源文件\res\layout\activity_main_lauch.xml
\res\layout-v21\activity_main_lauch.xml
我正在修改单个文件,因此抛出错误。 当我在两个文件中应用更改时,它开始工作。 希望对您有所帮助。