2015-11-07 82 views
2

其实我卡在这个错误在过去的24小时,仍然无法找到解决办法 错误说明:谷歌Android广告错误(?空引用)

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); 
..... 

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

回答

4

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

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

并做一个干净的构建。

+0

我重新启动整个食,但没有锁定然后我看到你的答案,我只是干净,现在它的工作,谢谢你! – Souf

1

在XML替换该

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

,而不是

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

没有解决它! ?:( – Souf

-1

你需要把这个变成你的com.google.android.gms.ads.AdView在你的LinearLayout:

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

编辑: 你需要拉你的测试设备的ID号来自LogCat。只需使用AdManager方法并将其设置为TEST_EMULATOR,就像logcat所说的那样。如果你使用USB调试在实际的设备上运行并观看logcat,则ID将出现在那里。

+0

总是同样的错误:( – Souf

+0

把你送到正确的权限在您的清单文件 – Stanojkovic

+0

什么权限 – Souf

0

我遇到过类似的问题。当我在国土资源文件密切检查有我的活动二国土资源文件:

\水库\布局\ activity_main_lauch.xml

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

我对矫正单文件,因此它是抛出错误。 当我在两个文件开始工作时应用更改。 希望它可以帮助。