2012-07-20 45 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"/>