开发者

Android Adwhirl 3.0 layout issue

开发者 https://www.devze.com 2023-03-01 14:48 出处:网络
Hi I\'m trying to get the new android 3.0 adwhirl working and I\'m struggling. In my xml I have: In my Class

Hi I'm trying to get the new android 3.0 adwhirl working and I'm struggling.

In my xml I have:

In my Class

...

setContentView(R.layout.main);

    //Add Whirl
    AdWhirlManager.setConfigExpireTimeout(1000 * 60 * 5);

    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();

    ...

However when I run it I get

The specified child already has a parent. You must call removeView() on the child's parent first.

I know it's probably a simple layout issue, but I'can't see a solution, any help would be appreciated.

Thanks friends.


I actually did the same thing as you when I was trying to get the AdWhirl sample code working. The problem is you've created a layout w/ the AdWhirlLayout in the XML file and you're retrieving that existing instance:

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

And then you're attempting to add it back into the LinearLayout you've got:

layout.addView(adWhirlLayout, layoutParams);

You can just drop the AdWhirlLayout from the XML file and replace the first line of code I mentioned w/ one creating a new instance:

AdWhirlLayout adWhirlLayout = new AdWhirlLayout();  

(I might be missing params for the constructor, I'm writing this out by hand on SO.)

That'll create a new instance of the AdWhirlLayout and add it to your layout_main LinearLayout.

0

精彩评论

暂无评论...
验证码 换一张
取 消