2010-12-14 112 views
0

我在我们的应用程序中第一次使用Adwhirl,但没有喜悦.. Adwhirl提供了这个演示代码,但它提示错误“java.lang.ClassCastException:android.widget.LinearLayout”在下面的选定行请AdWhirl没有渲染Android

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.layout_ad);*** 

     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.test_layout); 

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

这里XML

<LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:gravity="bottom" android:id="@+id/layout_ad" /> 

感谢

回答

0

改变这一行

AdWhirlLayout adWhirlLayout = (AdWhirlLayout) findViewById(R.id.layout_ad);// change this 

LinearLayout adWhirlLayout = (LinearLayout) findViewById(R.id.layout_ad); 
+0

SAURABH,它不工作... – 2010-12-21 08:07:39

+0

我不知道什么是AdWhilrLayout但例外是因为你的类型转换的线性布局AdWhirlLayout – ingsaurabh 2010-12-23 13:01:55

0

在布局XML中,布局的AdWhirl不应定义为LinearLayout,但作为AdWhirlLayout

不要忘了XML命名空间添加到布局文件的根元素:

xmlns:app="http://schemas.android.com/apk/res/com.adwhirl" 

com.adwhirl.AdWhirlLayout元素本身可能是一个LinearLayout中存在虽然。

下面是一个XML片断例如:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res/com.adwhirl 
    ... other LinearLayout attributes here"> 

<com.adwhirl.AdWhirlLayout 
    android:id="@+id/adwhirl_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 

    ...other elements here