更改AdView位置时出现ClassCastException

时间:2012-09-27 11:48:19

标签: java android android-layout

我正在尝试在SherlockFragmentActivity(带标签)中更改AdView的位置。现在,AdView显示在屏幕底部,我想更改AdView的位置,以便它显示在ActionBar标签和片段布局之间。

这是我的布局,屏幕底部有广告(这很好用):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

    <com.google.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/adview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/admod_id" />

</LinearLayout>

这就是我试图改变它的方式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.google.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/adview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/admod_id" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
    </FrameLayout>

</LinearLayout>

此更改会在ClassCastExeption方法的以下代码行中生成onCreate()

        adView = (AdView) findViewById(R.id.adview);

出现此错误消息:

        Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to com.google.ads.AdView

如果有人能够解释为什么会导致ClassCastException以及我如何解决这个问题,那将会很棒。

1 个答案:

答案 0 :(得分:11)

更改地点后尝试清理项目。

相关问题