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
精彩评论