2013-07-15 21 views
2

這是我試圖編譯的AdMob項目,但我沒有很多運氣。我有AdMob網站的發佈商ID以及一切。問題是,當我運行它立即崩潰。我會發布我的代碼併爲你登錄貓。謝謝!Android Admob不會運行?

繼承人鏈接到日誌貓:

http://imageshack.us/f/29/z5t.png/

這是我的MainActivity.java文件看起來像:

package com.example.testingitgood; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.LinearLayout; 

import com.google.ads.AdRequest; 
import com.google.ads.AdSize; 
import com.google.ads.AdView; 

public class MainActivity extends Activity { 

    AdView adView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Create an ad. 
     adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxxxxxx"); 

     // Add the AdView to the view hierarchy. The view will have no size 
     // until the ad is loaded. 
     LinearLayout layout = (LinearLayout) findViewById(R.id.main_layout); 
     layout.addView(adView); 

     // Create an ad request. Check logcat output for the hashed device ID to 
     // get test ads on a physical device. 
     AdRequest adRequest = new AdRequest(); 
     adRequest.addTestDevice(AdRequest.TEST_EMULATOR); 

     // Start loading the ad in the background. 
     adView.loadAd(adRequest); 

    } 

    @Override 
    public void onDestroy() { 
     if (adView != null) { 
      adView.destroy(); 
     } 
     super.onDestroy(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

這是我的activity_main.xml中文件的樣子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:id="@+id/main_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <com.google.ads.AdView 
     android:id="@+id/adView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     ads:adSize="BANNER" 
     ads:adUnitId="xxxxxxxxxxxxxxx" 
     ads:loadAdOnCreate="true" 
     ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" /> 

</LinearLayout> 

這就是我的AndroidManifest.xml文件lo像:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.testingitgood" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.testingitgood.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.google.ads.AdActivity" 
       android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> 
    </application> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

</manifest> 
+0

做一個項目乾淨,確保Admob庫包含在BuildPath中。 –

+0

@AndyRes是的,我確實包含了AdMob庫。 http://img10.imageshack.us/img10/9343/g70w.png –

回答

0

對我來說,看起來你是在佈局中創建AdView,然後在你的活動中重新創建它。嘗試將您在XML中創建的對象解析爲您在活動中創建的對象。

AdView ad =(AdView)findViewById(R.id.adView); 
ad.loadAd(new AdRequest().addTestDevice("<test device id goes here>")); 
+0

打開時仍出現錯誤。 :/。我是否只想在其中一個或另一個擁有廣告? –

+0

Mhmm:/ 與大多數情況一樣,adview應該在XML中「聲明」並給定一個id。然後在活動中,我們創建一個空的adview,並通過調用我們提供的ID來指定我們在XML中創建的一個。 不知道爲什麼這不起作用.. – CptRayMar