我正在尝试将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)
行上,所以我试图删除View()作为异常消息建议以及removeAllViews(),但我仍然收到此错误。
以下是Java文件:
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:
<?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>
和错误:
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
答案 0 :(得分:2)
您的AdWhirlLayout实际上已在您的XML中定义,因此您无需再次添加它(您可以删除layout.addView(adWhirlLayout, layoutParams);
行。只要您正确设置了ADWHIRL_KEY
在您的清单文件中,这应该可以正常工作。