dunker
dunker

Reputation: 97

Android How can i add ads in listactivity?

I tried both admob and mobfox. However, there is no advertisement in list activity.

RelativeLayout adslayout;
adapter = new ArrayAdapter<String>(this,R.layout.listlayout,R.id.txtmesajlar , messages);
mobfoxView = new MobFoxView(this, publisherId, true, true);
setListAdapter(adapter);
adslayout = (RelativeLayout)findViewById(R.id.adslayout);
adslayout.addView(mobfoxView);

From Logcat: Exception is java lang NullPointerException.

When i tried same codes in my other activity, i put mobfoxview in linearlayout and ads banner succesfully added. Is listactivity ignore advertisement or there is some error from me?

listlayout.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout android:layout_height="wrap_content" android:id="@+id/adslayout" android:layout_width="fill_parent" android:layout_gravity="center"></RelativeLayout>
<TextView android:layout_height="wrap_content" android:id="@+id/txtmesajlar" android:layout_width="fill_parent" android:textColor="@color/White" android:textSize="18sp" android:textStyle="bold" android:drawingCacheQuality="auto" android:drawableLeft="@drawable/listenvelop"></TextView>
</LinearLayout>

LOGCAT

08-25 12:11:59.925: ERROR/AndroidRuntime(887): FATAL EXCEPTION: main 08-25 12:11:59.925: ERROR/AndroidRuntime(887): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mc.bayrammesajlari/com.mc.bayrammesajlari.BayramActivity}: java.lang.NullPointerException 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at android.os.Handler.dispatchMessage(Handler.java:99) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at android.os.Looper.loop(Looper.java:123) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at android.app.ActivityThread.main(ActivityThread.java:3683) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at java.lang.reflect.Method.invokeNative(Native Method) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at java.lang.reflect.Method.invoke(Method.java:507) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at dalvik.system.NativeStart.main(Native Method) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): Caused by: java.lang.NullPointerException 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at com.mc.bayrammesajlari.BayramActivity.onCreate(BayramActivity.java:52) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 08-25 12:11:59.925: ERROR/AndroidRuntime(887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

Many thanks.

Upvotes: 1

Views: 1093

Answers (1)

Dan Dyer
Dan Dyer

Reputation: 54475

If you want to put ads next to your list, the answers to this question explain how to do it (PreferenceActivity is just a subclass of ListActivity so the solution is the same).

If you want the ads to actually appear in the list (as rows that scroll with the rest of the list content), I previously wrote this article about how I achieved this for AdMob.

Upvotes: 1

Related Questions