2012-07-20 65 views
0

我正在爲Android開發遊戲的早期過程,並且正在嘗試使用AdMob合併廣告橫幅。我已經從官方site的教程中直接下載了示例,所以我猜想無論我在這裏做錯了什麼,都必須是基本的,因爲在我的Galaxy S2設備上進行調試時,它會在幾秒鐘後崩潰。求助。AdMob簡單教程不起作用

package com.google.example.ads.fundamentals; 

import com.google.ads.AdRequest; 
import com.google.ads.AdSize; 
import com.google.ads.AdView; 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.telephony.TelephonyManager; 
import android.widget.LinearLayout; 

/** 
* A simple {@link Activity} that embeds an AdView. 
*/ 
public class BannerSample extends Activity { 

    private AdView adView; 
    private final TelephonyManager tm = 
     (TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE); 

    private final String AD_MOB_ID = "my AdMob ID goes here"; 

/** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

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

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

    AdRequest adRequest = new AdRequest(); 
    adRequest.addTestDevice(tm.getDeviceId()); 

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

    /** Called before the activity is destroyed. */ 
    @Override 
    public void onDestroy() { 
    // Destroy the AdView. 
    if (adView != null) { 
     adView.destroy(); 
    } 

    super.onDestroy(); 
    } 
} 

在屏幕截圖的logcat的數據是here

編輯:另外加入的manifest.xml,我懷疑是什麼causeing的問題 - 令人驚訝的是與例如來自官方網站的一個在它有一個錯誤(根據Eclipse的),所以我不得不稍作修改:

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

    <uses-sdk android:minSdkVersion="3" 
       android:targetSdkVersion="13" /> 
     <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name"> 
     <activity 
      android:name=".HelloAdMobActivity" 
      android:label="@string/app_name" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
      android:screenOrientation="landscape" > 
      <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" 
       android:screenOrientation="landscape" > 
     </activity> 
    </application> 

</manifest> 
+0

歡迎來到Stack Overflow!你有關於崩潰的logcat信息嗎? – gobernador 2012-07-20 23:32:37

回答

1

好吧,事實證明問題出在tm.getDeviceId()之間,無論出於什麼原因導致程序粉碎。

1

logcat的信息將是超級有用,但我打賭這兩個原因之一:

  1. 您將SDK引用爲外部Jar,並未將其添加到您的libs/文件夾中。您有兩個選擇來解決此問題:將其添加到您的libs/文件夾,或者轉到屬性 - > Java構建路徑 - >訂單和導出,然後檢查AdMob jar。
  2. 您的XML中沒有LinearLayout,其中包含android:id="linearLayout"。這個樣本項目應該包括這個可能性較小。
+0

你好,謝謝你的回答。我在Properties - > Java Build Path - > Order and Export中檢查了AdMob jar,並且我的XML中有一個帶有id的LinearLayout,就像你說的。不幸的是,它仍然崩潰。該應用程序會在藍色背景上加載顯示爲「Android基礎版」文字的橫幅,並在幾秒鐘內消失。然後,應用程序就會彈出一個「不幸的AndroidFundamentals停止」的消息。另外,如果我嘗試點擊「後退」按鈕,它會在幾秒鐘後不響應並且將其擊碎。 – 2012-07-21 10:03:12

+0

我想給你更多的信息,但我不知道還有什麼補充 - 我完全按照教程的建議。 – 2012-07-21 10:04:33

+0

對不起,我將Logcat附加到了這個問題上 – 2012-07-21 11:19:54

0

您需要添加以下權限。

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