java - 更改 AdView 位置时出现 ClassCastException

标签 java android android-layout

我正在尝试更改我的 AdView 在 SherlockFragmentActivity(带有标签)中的位置。由于现在 AdView 显示在屏幕底部,我想更改 AdView 的位置,使其显示在 ActionBar< 之间 选项卡和 fragment 布局。

这是我在屏幕底部放置广告的布局(效果很好):

<?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>

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

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

出现此错误消息:

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

如果有人能解释为什么这会导致 ClassCastException 以及我将如何解决这个问题,那就太好了。

最佳答案

尝试在更改位置后清理项目。

关于java - 更改 AdView 位置时出现 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12620685/

相关文章:

java - 禁用Java中的库?

java - 解析日期格式异常

java - 如何通过 JPanel 构造函数中的 String 参数更改 JPanel 的背景颜色?

java - AndEngine - 无法实例化 Activity

android - 抽屉导航汉堡图标只有在手动滑动抽屉后才会显示

java - Android - 如何将接口(interface)传递给AsyncTask

android - 一起控制两个ViewPager

c# - "Read .JSON file"统一 C# 中的方法,也适用于 IOS 和 Android 平台

android - TextView 未显示在 Google map 的顶部

android - 如何替换旧 API<21 中的 NestedScrollView?