2012-03-19 50 views
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(),但我仍然得到這個錯誤。AdWhirl導致IllegalStateException

下面是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

我們需要你的堆棧跟蹤和罪魁禍首高亮顯示。 – Snicolas 2012-03-19 19:34:35

+0

我會如何突出罪魁禍首?我在開頭提到它 – Stephanie 2012-03-19 19:39:35

回答

2

你AdWhirlLayout實際上已經定義了XML,所以你不需要再次添加它(你可以刪除layout.addView(adWhirlLayout, layoutParams);行。只要你有等你的清單文件中正確地ADWHIRL_KEY,這應該正常工作。

+0

當我刪除該行時,它會運行,但除了隨機文本視圖添加了「下面的AdWhirlLayout」之外,沒有任何內容顯示。我在清單文件中有adwhirl鍵。 – Stephanie 2012-03-19 19:42:19

+0

我只是看着logcat,意識到它沒有顯示,因爲沒有輪播廣告。 – Stephanie 2012-03-19 20:01:19

相關問題