English 中文(简体)
Problems with AdMob integration into Android App
原标题:

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>
问题回答

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




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签