0

我是 admob 的新手,我有一些错误,我很困惑....有人可以帮忙吗?每次我启动应用程序时,它都会强制退出。我知道该应用程序本身可以工作,因为我之前在没有 admob 的情况下尝试过它......这是我的主要课程:package 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>

我很困惑......请帮忙!

4

1 回答 1

0

首先,根据文档,您需要在清单中使用它的 configChanges 指定 AdActivity:

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

此外,您的 main.xml 需要有一个LinearLayoutid 元素mainLayout。否则程序在你调用时崩溃layout.addView(adView);

最后,仅供参考,以下方法不起作用:

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

设备 ID 将是一个 32 位十六进制散列字符串。您需要查看 logcat 以获取日志记录语句:

To get test ads on this device, call adRequest.addTestDevice("some_32_hex_string")

请输入该字符串。

于 2012-06-07T16:55:17.193 回答