编程语言
首页 > 编程语言> > java – Android google ads错误(null引用?)

java – Android google ads错误(null引用?)

作者:互联网

我实际上在最后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);
.....

我在这里看不到任何错误我检查了所有导入库一切正常
请帮帮我,我的时间不多了:/

解决方法:

它失败了,因为它没有在您的布局中找到具有id adView的元素.

确保上面显示的XML文件实际上是activity_main.xml.

并做一个干净的构建.

标签:java,android,admob,adview
来源: https://codeday.me/bug/20190528/1168622.html