簡體   English   中英

Android google ads錯誤(null引用?)

[英]Android google ads error (null reference ?)

我實際上在最后24小時陷入此錯誤仍然無法找到解決方案的錯誤說明:

11-06 18:53:37.745:E / AndroidRuntime(1698):引起:java.lang.NullPointerException:嘗試調用虛擬方法'void com.google.android.gms.ads.AdView.loadAd(com.google。在一個空對象引用上的'android.gms.ads.AdRequest)'

這是一些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

並做一個干凈的構建。

在XML中替換它

xmlns:ads="http://schemas.android.com/apk/res-auto"

代替

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

我面臨類似的問題。 當我在資源文件中仔細檢查時,我的活動有兩個資源文件:

\\水庫\\布局\\ activity_main_lauch.xml

\\水庫\\布局-V21 \\ activity_main_lauch.xml

我正在修改單個文件,因此它拋出了錯誤。 當我在兩個文件中應用更改時,它開始工作。 希望它可能有所幫助。

您需要將它放入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將顯示在那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM