首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AdWhirl导致IllegalStateException

AdWhirl导致IllegalStateException
EN

Stack Overflow用户
提问于 2012-03-20 03:30:42
回答 1查看 236关注 0票数 0

我正在尝试将AdWhirl添加到我的应用程序中,但我决定从一个简单的"HelloWorld“开始,以确保我知道我在做什么。果然,我不..。我从the AdWhirl Android SDK Setup page获得了这段代码,但是当我尝试运行它时,我得到了这样的异常:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.它发生在layout.addView(adWhirlLayout, layoutParams)行上,所以我按照异常消息的建议尝试了removeView()以及removeAllViews(),但是我仍然得到了这个错误。

下面是Java文件:

代码语言:javascript
复制
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");
    }
}

这是我的main.xml:

代码语言:javascript
复制
<?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>

以及错误:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-20 03:40:23

您的AdWhirlLayout实际上已经在您的XML中定义了,所以您不需要再次添加它(您可以删除layout.addView(adWhirlLayout, layoutParams);行。只要在清单文件中正确设置了ADWHIRL_KEY,就可以正常工作。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9776620

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档