0

I am trying to add AdWhirl to my app, but I decided to start out with a simple "HelloWorld" to make sure I know what I'm doing. Sure enough, I don't... I got this code from the AdWhirl Android SDK Setup page, but when I try to run it, I get this exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. It occurs on the line layout.addView(adWhirlLayout, layoutParams) so I've tried to removeView() as the exception message suggests as well as removeAllViews() but still I get this error.

Below is the Java file:

package hello.adwhirl;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.adwhirl.AdWhirlLayout;
import com.adwhirl.AdWhirlLayout.AdWhirlInterface;
import com.adwhirl.AdWhirlManager;
import com.adwhirl.AdWhirlTargeting;

public class HelloAdWhirl extends Activity implements AdWhirlInterface {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        AdWhirlManager.setConfigExpireTimeout(1000 * 60 * 5);

        AdWhirlTargeting.setAge(23);
        AdWhirlTargeting.setGender(AdWhirlTargeting.Gender.MALE);
        AdWhirlTargeting.setKeywords("online games gaming");
        AdWhirlTargeting.setPostalCode("94123");
        AdWhirlTargeting.setTestMode(false);

        AdWhirlLayout adWhirlLayout = (AdWhirlLayout) findViewById(R.id.adwhirl_layout);

        TextView textView = new TextView(this);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        int diWidth = 320;
        int diHeight = 52;
        int density = (int) getResources().getDisplayMetrics().density;

        adWhirlLayout.setAdWhirlInterface(this);
        adWhirlLayout.setMaxWidth((int) (diWidth * density));
        adWhirlLayout.setMaxHeight((int) (diHeight * density));

        layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        textView.setText("Below AdWhirlLayout");

        LinearLayout layout = (LinearLayout) findViewById(R.id.layout_main);

        layout.setGravity(Gravity.CENTER_HORIZONTAL);
        layout.addView(adWhirlLayout, layoutParams);
        layout.addView(textView, layoutParams);
        layout.invalidate();
    }

    public void adWhirlGeneric() {
        // TODO Auto-generated method stub
        System.out.println("Do stuff");
    }
}

This is my main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/layout_main">
<com.adwhirl.AdWhirlLayout
    android:layout_height="wrap_content" android:layout_width="fill_parent"
    android:id="@+id/adwhirl_layout"></com.adwhirl.AdWhirlLayout>
</LinearLayout>

and the error:

03-19 15:35:54.218: ERROR/AndroidRuntime(3510): FATAL EXCEPTION: main
03-19 15:35:54.218: ERROR/AndroidRuntime(3510): java.lang.RuntimeException: Unable to start activity ComponentInfo{hello.adwhirl/hello.adwhirl.HelloAdWhirl}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2833)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.app.ActivityThread.access$2300(ActivityThread.java:136)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.os.Looper.loop(Looper.java:143)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.app.ActivityThread.main(ActivityThread.java:5061)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at java.lang.reflect.Method.invoke(Method.java:521)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at dalvik.system.NativeStart.main(Native Method)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.view.ViewGroup.addViewInner(ViewGroup.java:1992)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.view.ViewGroup.addView(ViewGroup.java:1887)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.view.ViewGroup.addView(ViewGroup.java:1867)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at hello.adwhirl.HelloAdWhirl.onCreate(HelloAdWhirl.java:50)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1066)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2797)
03-19 15:35:54.218: ERROR/AndroidRuntime(3510):     ... 11 more
Stephanie
  • 129
  • 3
  • 13

1 Answers1

2

Your AdWhirlLayout is actually already defined in your XML, so there is no need for you to add it again (you can remove the layout.addView(adWhirlLayout, layoutParams); line. As long as you've set your ADWHIRL_KEY properly in your manifest file, this should work properly.

RajPara
  • 2,281
  • 1
  • 16
  • 9
  • When I remove that line, it runs but nothing shows except the random text view they added that says "Below AdWhirlLayout." I do have the adwhirl key in the manifest file. – Stephanie Mar 19 '12 at 19:42
  • I just looked at the logcat and realized it doesn't show because there are no ads in rotation. – Stephanie Mar 19 '12 at 20:01