0

我第一次在我们的应用程序中使用 Adwhirl,但没有任何乐趣.. Adwhirl 提供了这个演示代码,但它在下面的选定行上提示错误“java.lang.ClassCastException:android.widget.LinearLayout”

    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" />

谢谢

4

2 回答 2

0

将此行更改为

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

LinearLayout adWhirlLayout = (LinearLayout ) findViewById(R.id.layout_ad);
于 2010-12-16T09:50:23.333 回答
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
于 2011-05-21T11:34:30.600 回答