Adding Admob to Android App : AdView
I am a hair away (i think) from finally getting ads into my app. but am getting crashes at 1 line. See code:
public class ... extends ListActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
String[] rmenu = getResources().getStringArray(R.array.root_menu);
if (rmenu != null) {
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, rmenu));
}
setContentView(R.layout.main);
//
// Admob Code
// Create the adView
AdView av = new AdView(this, AdSize.BANNER, "publisher_id");
// 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(av);
// Initiate a generic request to load it with an ad
av.loadAd(new AdRequest());
}
The crash is happening at the line: layout.addView(av);
I have an adview added to the layout file referenced above (mainLayout). I will add that xml code as well below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000" >
<adView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text="Loading..." />
</LinearLayout>
Any help would be much appreciated! Or just a finger in the right direction.
Logcat file::
'code'04-22 18:00:29.862: DEBUG/AndroidRuntime(307): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-22 18:00:29.862: DEBUG/AndroidRuntime(307): CheckJNI is ON
04-22 18:00:30.002: DEBUG/AndroidRuntime(307): --- registering native functions ---
04-22 18:00:30.642: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.xxxxx/.xxxxx }
04-22 18:00:30.762: DEBUG/AndroidRuntime(307): Shutting down VM
04-22 18:00:30.772: DEBUG/dalvikvm(307): Debugger has detached; object registry had 1 entries
04-22 18:00:30.852: INFO/ActivityManager(59): Start proc com.xxxxx for activity com.xxxxx/.xxxxx: pid=314 uid=10036 gids={3003}
04-22 18:00:30.872: INFO/AndroidRuntime(307): NOTE: attach of thread 'Binder Thread #3' failed
04-22 18:00:31.812: DEBUG/AndroidRuntime(314): Shutting down VM
04-22 18:00:31.812: WARN/dalvikvm(314): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): FATAL EXCEPTION: main
04-22 18:00:31.842: ERROR/AndroidRuntime(314): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxx/com.xxxxx.xxxxx}: java.lang.NullPointerException
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread$H.handleMessage(Act开发者_Python百科ivityThread.java:2033)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.os.Looper.loop(Looper.java:123)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at java.lang.reflect.Method.invoke(Method.java:521)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at dalvik.system.NativeStart.main(Native Method)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): Caused by: java.lang.NullPointerException
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at com.xxxxx.xxxxxx.onCreate(txchl.java:36)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): ... 11 more
=========== UPDATED =============================
FIXED.
Ok, I am not getting anymore crashes, and I am seeing ads pop in after about a few minutes!!
Here is what I did. At http://code.google.com/mobile/ads/docs/android/fundamentals.htmlthis tutorial You will see that the frst step was to Add the SDK JAR to your project. I was using the newer JAR file available for download at first, titled "googleadmobadssdkandroid". I still had the older download available, titled "admob-sdk-android_20101109", and I added BOTH JAR files to my project and the code above now works.
Your LinearLayout appears to be missing the second line in the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/your.package.name"
Then, have you added the Jar to your build path and is it located in your "libs" folder?
Lastly, I will sometimes forget to create the attrs.xml file in res/values
FIXED.
Ok, I am not getting anymore crashes, and I am seeing ads pop in after about a few minutes!!
Here is what I did. At http://code.google.com/mobile/ads/docs/android/fundamentals.htmlthis tutorial You will see that the frst step was to Add the SDK JAR to your project. I was using the newer JAR file available for download at first, titled "googleadmobadssdkandroid". I still had the older download available, titled "admob-sdk-android_20101109", and I added BOTH JAR files to my project and the code above now works.
精彩评论