Admob新手,不工作

时间:2012-06-07 04:33:28

标签: android admob

我是admob的新手,我有一些错误,我很困惑....有人可以帮忙吗?每次我启动应用程序它只是强制退出。我知道应用程序本身是有效的,因为我之前没有尝试过它...这是我的主要课程: 包com.firecow.markcheck;

    package com.firecow.markcheck;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.google.ads.*;

public class MarkCheck extends Activity {
    /** Called when the activity is first created. */

    Button sub, share;
    EditText per;
    Context context;
    CharSequence text;
    String heightString;
    double num;
    Toast toast;
    MediaPlayer player;
    private AdView adView;
    AdRequest adRequest = new AdRequest();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        sub = (Button) findViewById(R.id.bSubmit);
        share = (Button) findViewById(R.id.bShare);
        per = (EditText) findViewById(R.id.tePercent);
        context = getApplicationContext();
        final int duration = Toast.LENGTH_SHORT;
        adRequest.addTestDevice(AdRequest.TEST_EMULATOR);               // Emulator
        adRequest.addTestDevice("EMJY-KBBS-VL7P-2");                      // Test Android Device

        //Create the adView
        adView = new AdView(this, AdSize.BANNER, "a14fd022edb48e8");

        // 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(adRequest);

        text = "Made by Firecow, Voice of William Wang";
        toast = Toast.makeText(context, text, duration);
        toast.show();

        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                heightString = per.getText().toString();
                if (heightString != null) {
                    num = Double.parseDouble(heightString);

                    if (num >= 151) {
                        text = "I didn't know you could score that high...";
                        toast = Toast.makeText(context, text, duration);
                        toast.show();
                    } else if (num >= 101) {
                        text = "You get a A+";
                        toast = Toast.makeText(context, text, duration);
                        player = MediaPlayer.create(getApplicationContext(),
                                R.raw.perfect);
                        player.start();
                        toast.show();
                    } else if (num >= 86) {
                        text = "You get a A";
                        toast = Toast.makeText(context, text, duration);
                        player = MediaPlayer.create(getApplicationContext(),
                                R.raw.harvard);
                        player.start();
                        toast.show();
                    } else if (num >= 73) {
                        text = "You get a B";
                        toast = Toast.makeText(context, text, duration);
                        player = MediaPlayer.create(getApplicationContext(),
                                R.raw.ucla);
                        player.start();
                        toast.show();
                    } else if (num >= 68) {
                        text = "You get a C+";
                        toast = Toast.makeText(context, text, duration);
                        player = MediaPlayer.create(getApplicationContext(),
                                R.raw.kwantlan);
                        player.start();
                        toast.show();
                    } else if (num >= 60) {
                        text = "You get a C";
                        toast = Toast.makeText(context, text, duration);
                        player = MediaPlayer.create(getApplicationContext(),
                                R.raw.wendys);
                        player.start();
                        toast.show();
                    } else if (num >= 50) {
                        text = "You get a C-";
                        toast = Toast.makeText(context, text, duration);
                        player = MediaPlayer.create(getApplicationContext(),
                                R.raw.drop_out);
                        player.start();
                        toast.show();
                    } else if (num <= 49) {
                        text = "You get a F";
                        toast = Toast.makeText(context, text, duration);
                        player = MediaPlayer.create(getApplicationContext(),
                                R.raw.failure);
                        player.start();
                        toast.show();
                    } else {
                        text = "Please type a proper percent";
                        toast = Toast.makeText(context, text, duration);
                        toast.show();
                    }
                } else {
                    text = "Please enter a proper percent";
                    toast = Toast.makeText(context, text, duration);
                    toast.show();
                }

            }
        });

        share.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent bacon = new Intent("com.firecow.markcheck.SHARE");
                startActivity(bacon);

            }
        });
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        try {
            if (adView != null) {
                adView.destroy();
            }
            player.release();

            finish();
        } catch (Exception E) {

        }
    }
}

和我的清单:

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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MarkCheck"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Share"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.firecow.markcheck.SHARE" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.ads.AdActivity"
            />
    </application>

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

</manifest>

我太困惑了......请帮忙!

1 个答案:

答案 0 :(得分:0)

首先,根据documentation,您需要在清单中使用configChanges指定AdActivity:

<activity android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

此外,您的main.xml需要一个标识为LinearLayout的{​​{1}}元素。否则,当您拨打mainLayout

时,程序会崩溃

最后,仅供参考,以下内容不起作用:

layout.addView(adView);

设备ID将是32个十六进制哈希字符串。您需要查看logcat以获取日志声明:

adRequest.addTestDevice("EMJY-KBBS-VL7P-2");

输入该字符串。