AdMob 集成到 Android 应用程序中的问题

发布于 2024-10-07 06:19:22 字数 5680 浏览 0 评论 0原文

我对 Java 和 Android 应用程序还很陌生,所以,虽然这对其他人来说可能很简单,但它却让我有些头痛。

该应用程序应该加载、播放声音文件,然后关闭。而且无需 AdMob 即可正常工作。

使用 AdMob 时,它会启动,振动一次(普通版本中不会),正常运行,然后卡住,振动 3 次,然后关闭并抛出“抱歉!应用程序名称(进程 PROCESS.NAME)已意外停止。请”再试一次”。

我正在使用我的第二个应用程序,也是我的第一个 AdMob 集成。

我遵循的 SDK 说明:http://www.admob.com/docs/AdMob_Android_SDK_Instructions.pdf

我做了什么:
- 已注册
- 获取 SDK
- 添加了 .jar
- 在 AndroidManifest.xml 末尾的 SDK 说明中添加了 pub IDAdMobActivity 定义跟踪市场安装代码,并编辑了 pub ID
- 添加了互联网权限
- 还添加了“ADMOB_ALLOW_LOCATION_FOR_ADS”
- 将“AdMob AdView 属性”添加到 attrs.xml(我使用 Eclipse,所以我首先尝试将其添加到 res/values/strings.xml,然后创建一个新的 xml 并向其中添加代码)



如果需要完整的代码,我将编辑这篇文章。如有任何帮助,我们将不胜感激。
预先感谢您
克里斯


(LogCat 在没有 AdMob 文件的情况下抛出两个小错误(我 //-ed 导入和 AdView ))
(LogCat 与 AdMob 会抛出此错误)

12-11 14:50:00.266: ERROR/beep(284): started0
12-11 14:50:00.346: ERROR/AndroidRuntime(284): Uncaught handler: thread main exiting due to uncaught exception
12-11 14:50:00.368: ERROR/AndroidRuntime(284): java.lang.RuntimeException: Unable to start activity ComponentInfo{seven.kitty.purr/seven.kitty.purr.KittyPurr}: java.lang.NullPointerException
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.os.Looper.loop(Looper.java:123)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread.main(ActivityThread.java:4363)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at java.lang.reflect.Method.invoke(Method.java:521)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at dalvik.system.NativeStart.main(Native Method)
12-11 14:50:00.368: ERROR/AndroidRuntime(284): Caused by: java.lang.NullPointerException
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at seven.kitty.purr.KittyPurr.onCreate(KittyPurr.java:20)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     ... 11 more
12-11 14:50:00.407: ERROR/dalvikvm(284): Unable to open stack trace file '/data/anr/traces.txt': Permission denied



使用完整的 .java 和 XML 代码进行编辑。我不喜欢 Java,它与我通常使用的 AS、PHP、JavaScript 和其他 Web 语言有很大不同

KittyPurr.java

package seven.kitty.purr;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.admob.android.ads.AdManager;
import com.admob.android.ads.AdView;

public class KittyPurr extends Activity
{
  private MediaPlayer mMediaPlayer;
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
   playAudio();



  }

  private void playAudio () {
    try {
    mMediaPlayer = MediaPlayer.create(this, R.raw.purrr);
    mMediaPlayer.setLooping(false);
    Log.e("beep","started0");
    mMediaPlayer.start();

    AdView adView = (AdView)findViewById(R.id.ad);
    adView.requestFreshAd();

    mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer arg0) {
         finish();
      }
    });
    } catch (Exception e) {
    Log.e("beep", "error: " + e.getMessage(), e);
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if (mMediaPlayer != null) {
    mMediaPlayer.release();
    mMediaPlayer = null;
    }
  }
}



main.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/seven.kitty.purr"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<com.admob.android.ads.AdView
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>



属性.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.admob.android.ads.AdView">
<attr name="backgroundColor" format="color" />
<attr name="primaryTextColor" format="color" />
<attr name="secondaryTextColor" format="color" />
<attr name="keywords" format="string" />
<attr name="refreshInterval" format="integer" />
</declare-styleable>
</resources>

I am quite new into Java and Android Apps, so , while this may be cake for others it gives me some headache .

The app should load , play a sound file , and close . And it works fine without the AdMob .

With the AdMob it starts , vibrates once ( it doesnt in the normal version ) , runs normally , and then gets stuck , vibrates 3 times , and closes throwing a " Sorry! The application NAME ( process PROCESS.NAME ) has stopped unexpectedly . Please try again " .

I`m on my second app , and my first AdMob integration.

The SDK instructions I followed : http://www.admob.com/docs/AdMob_Android_SDK_Instructions.pdf

What I did :
- Registered
- Got the SDK
- Added the .jar
- Added the pub ID , AdMobActivity definition and Track Market Installs code from the SDK Instructions at the end of AndroidManifest.xml and edited the pub ID
- Added the internet permission
- Added "ADMOB_ALLOW_LOCATION_FOR_ADS" too
- Added "AdMob AdView Attributes" to attrs.xml ( I use Eclipse , so I first tried to add this to res/values/strings.xml , then made a new xml and added the code to it )

If full code is needed I will edit this post. Any help is appreciated .
Thank you in advance
Chris

(LogCat throws two small errors without the AdMob files ( I //-ed the import and AdView ) )
(LogCat WITH AdMob throws this )

12-11 14:50:00.266: ERROR/beep(284): started0
12-11 14:50:00.346: ERROR/AndroidRuntime(284): Uncaught handler: thread main exiting due to uncaught exception
12-11 14:50:00.368: ERROR/AndroidRuntime(284): java.lang.RuntimeException: Unable to start activity ComponentInfo{seven.kitty.purr/seven.kitty.purr.KittyPurr}: java.lang.NullPointerException
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.os.Looper.loop(Looper.java:123)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread.main(ActivityThread.java:4363)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at java.lang.reflect.Method.invokeNative(Native Method)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at java.lang.reflect.Method.invoke(Method.java:521)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at dalvik.system.NativeStart.main(Native Method)
12-11 14:50:00.368: ERROR/AndroidRuntime(284): Caused by: java.lang.NullPointerException
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at seven.kitty.purr.KittyPurr.onCreate(KittyPurr.java:20)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-11 14:50:00.368: ERROR/AndroidRuntime(284):     ... 11 more
12-11 14:50:00.407: ERROR/dalvikvm(284): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

Editing with full .java and XML codes . I suck at Java, it's so diferent than AS , PHP , JavaScript and other web languages I usually use

KittyPurr.java

package seven.kitty.purr;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.admob.android.ads.AdManager;
import com.admob.android.ads.AdView;

public class KittyPurr extends Activity
{
  private MediaPlayer mMediaPlayer;
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
   playAudio();



  }

  private void playAudio () {
    try {
    mMediaPlayer = MediaPlayer.create(this, R.raw.purrr);
    mMediaPlayer.setLooping(false);
    Log.e("beep","started0");
    mMediaPlayer.start();

    AdView adView = (AdView)findViewById(R.id.ad);
    adView.requestFreshAd();

    mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
            public void onCompletion(MediaPlayer arg0) {
         finish();
      }
    });
    } catch (Exception e) {
    Log.e("beep", "error: " + e.getMessage(), e);
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if (mMediaPlayer != null) {
    mMediaPlayer.release();
    mMediaPlayer = null;
    }
  }
}

main.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/seven.kitty.purr"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<com.admob.android.ads.AdView
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="com.admob.android.ads.AdView">
<attr name="backgroundColor" format="color" />
<attr name="primaryTextColor" format="color" />
<attr name="secondaryTextColor" format="color" />
<attr name="keywords" format="string" />
<attr name="refreshInterval" format="integer" />
</declare-styleable>
</resources>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘紸啶 2024-10-14 06:19:22

添加了罐子

意味着您将文件添加到 /libs 吗?如果没有,就这样做。

编辑:

您使用了错误的onCreate。应该是:

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 ...
}

Added the jar

means you add the file to /libs? If not, do so.

EDIT:

You are using the wrong onCreate. It should be:

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 ...
}
短暂陪伴 2024-10-14 06:19:22
you need to add the following this in xml and if you want to test it in emulator
  then u need to set the **adrequest.setTestDevice(true)**

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        **xmlns:myapp="http://schemas.android.com/apk/libs/com.google.ads"**
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <com.google.ads.AdView
    android:id="@+id/ad"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    **myapp:adUnitId="Your Admob ID"
    myapp:adSize="BANNER"**
    />
    </LinearLayout>


in the AndroidManifest.xml you need to add the following thing

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

and one thing you need to consider that u need to compile project with android 3.2 or above
you need to add the following this in xml and if you want to test it in emulator
  then u need to set the **adrequest.setTestDevice(true)**

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        **xmlns:myapp="http://schemas.android.com/apk/libs/com.google.ads"**
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <com.google.ads.AdView
    android:id="@+id/ad"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    **myapp:adUnitId="Your Admob ID"
    myapp:adSize="BANNER"**
    />
    </LinearLayout>


in the AndroidManifest.xml you need to add the following thing

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

and one thing you need to consider that u need to compile project with android 3.2 or above
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文