0

私はAndroid向けのゲームを開発する初期の段階にあり、AdMobを使用して広告バナーを組み込むことを試みています。公式サイトのチュートリアルから直接サンプルをダウンロードしたので、Galaxy S2デバイスでデバッグすると数秒後にクラッシュするため、ここで間違っていることは基本的なものでなければならないと思います。助けてください。

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データはこちらです

編集:また、問題の原因であると思われるManifest.XMLを追加します-驚くべきことに、公式サイトの例に付属しているものにエラーがあったため(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>
4

3 に答える 3

1

Logcatの情報は非常に便利ですが、私は次の2つの理由のいずれかに賭けています。

  1. SDKを外部Jarとして参照しましたが、libs/フォルダーに追加しませんでした。これを修正するには、2つのオプションがあります。フォルダに追加するlibs/か、[プロパティ]->[Javaビルドパス]->[注文とエクスポート]に移動してAdMobjarを確認します。
  2. XMLに。を使用したLinearLayoutがありませんandroid:id="linearLayout"。サンプルプロジェクトにこれを含める必要があるため、これは発生する可能性が低くなります。
于 2012-07-21T01:25:18.827 に答える
1

了解しました。問題はにあることがわかりましたtm.getDeviceId()。これにより、何らかの理由でプログラムがクラッシュしました。

于 2012-07-22T22:10:59.577 に答える
0

次の権限を追加する必要があります。

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
于 2013-05-20T14:50:21.940 に答える