1

我对 Java 和 Android 应用程序很陌生,所以,虽然这对其他人来说可能是小菜一碟,但它让我有些头疼。

该应用程序应该加载、播放声音文件并关闭。没有 AdMob 也能正常工作。

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

我正在使用我的第二个应用,以及我的第一个 AdMob 集成。

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

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



如果需要完整代码,我将编辑这篇文章。任何帮助表示赞赏。
提前谢谢你,
克里斯


(LogCat 在没有 AdMob 文件的情况下抛出两个小错误(我 //-ed 导入和 AdView))
(LogCat WITH 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 和其他网络语言如此不同

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;
    }
  }
}



主要的.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>
4

2 回答 2

0
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
于 2013-01-25T07:06:38.153 回答
0

添加了罐子

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

编辑:

你用错了onCreate。它应该是:

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 ...
}
于 2010-12-11T04:12:51.830 回答