0

在过去的两天里,我一直在尝试将 Admob 广告实施到为 Android 2.2 及更高版本开发的应用程序中。我已经在网上搜索并尝试了我找到的所有答案,但仍然没有。我遵循了 admob 网站上的所有说明,并尝试了 java(发布在下面)和 xml 版本。每次我使用 xml 时,都会收到错误消息“无法实例化 com.google.ads.AdView”。我已经尝试过修复,但它仍然崩溃。到目前为止,我已将“minSdkVersion”更改为 8(用于在 2.2 上开发),并将“目标”更改为 android=14,这是 AdMob sdk 所需的。任何帮助将非常感激!

菜单类

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

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

public class Menu extends Activity{

Button start, HTP;
private AdView adView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    start = (Button) findViewById(R.id.bStart);
    HTP = (Button) findViewById(R.id.bHTP);


    start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(Menu.this,
                    MyGameActivity.class);
            Menu.this.startActivity(myIntent);
            }
        });

    HTP.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(Menu.this,
                    HowToPlay.class);
            Menu.this.startActivity(myIntent);
        }
    });


    // Create the adView
    adView = new AdView(this, AdSize.BANNER, "publisher id in quotes");

    // Lookup your LinearLayout assuming it’s been given
    // the attribute android:id="@+id/mainLayout"
    LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);

    // Add the adView to it
    layout.addView(adView);

    // Initiate a generic request to load it with an ad
    adView.loadAd(new AdRequest());
  }

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

}

显现

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

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

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

<application
    android:icon="@drawable/ic_game"
    android:label="Cube Jump" >
    <activity
        android:name=".Splash"
        android:label="Cube Jump"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        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=".Menu"
        android:label="Cube Jump"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:screenOrientation="landscape" >
        <intent-filter>
            <action android:name="com.tacomaapps.MENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name=".MyGameActivity"
        android:label="Cube Jump"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:screenOrientation="landscape" >
    </activity>

    <activity
        android:name=".HowToPlay"
        android:label="Cube Jump"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:screenOrientation="landscape" >
    </activity>
    <activity android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>        
</application>

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/mainLayout"
android:layout_height="match_parent"
android:background="@drawable/menuback"
android:baselineAligned="true"
android:orientation="horizontal" >


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="210dp"
    android:layout_weight="1.0"
    android:weightSum="100" >

    <Button
        android:id="@+id/bFillSpace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="45"
        android:visibility="invisible"
        android:text="Button" />

    <Button
        android:id="@+id/bStart"
        android:layout_width="150dp"
        android:layout_height="63dp"
        android:layout_margin="5dp"
        android:layout_weight="5"
        android:background="@drawable/play" />

    <Button
        android:id="@+id/bHTP"
        android:layout_width="150dp"
        android:layout_height="63dp"
        android:layout_margin="5dp"
        android:layout_weight="5"
        android:background="@drawable/htp_button" />

    <Button
        android:id="@+id/bFillSpace2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="45"
        android:visibility="invisible"
        android:text="Button" />
</LinearLayout>

4

2 回答 2

0

您是否添加了此处提到的 jar 文件

于 2012-06-15T03:23:36.043 回答
0

您的代码非常好,可以在我的最后运行。

我希望您验证以下内容。

1您在项目中创建了“libs”文件夹,并在那里添加了 GoogleAdMobAdsSdk-XXXjar并从那里引用它。默认情况下,它们不存在。如果您直接从 \extras\google\admob_ads_sdk 引用 jar 文件,则程序会因 java.lang.NoClassDefFoundError: com.google.ads.AdViewStrange 但真实而崩溃。

你的项目结构应该是这样的

在此处输入图像描述

于 2012-06-15T04:42:14.937 回答