使用AdMob强制关闭;为Android 2.2开发

时间:2012-06-15 03:19:34

标签: android admob

我过去两天一直在尝试将Admob广告投放到正在为Android 2.2及更高版本开发的应用中。我在线搜索并尝试了我找到的所有答案,但仍然没有。我遵循了admob网站上的所有说明,并尝试了java(下面发布)和xml版本。每次我使用xml时,都会收到错误“无法实例化com.google.ads.AdView”。我已经尝试了修复,但它仍然崩溃。到目前为止,我已将“minSdkVersion”更改为8(用于2.2上的开发),并将“target”更改为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>

main.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>

2 个答案:

答案 0 :(得分:0)

您是否按照here

添加了jar文件

答案 1 :(得分:0)

你的代码非常好,并且在我的最后运行。

我希望您验证以下内容。

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

您的项目结构应该看起来像这样

enter image description here