我在自己的应用中集成Google AdMob广告时遇到问题。我使用导航抽屉布局,如下所示:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<fragment
android:id="@+id/navigation_drawer"
android:name="pl.ntimobile.personalcoach.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start" />
当我将google AdMob广告放在片段下方时,就像这样:
(...)
android:layout_gravity="start" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
我在课堂上正确启动它,效果很好,但广告显示在屏幕中间,2)在导航抽屉上方,所以当我显示导航抽屉时,我无法点击任何一个内容。 因此,当我尝试将广告和片段放在线性布局中时,我得到ClassCastException ...我的布局看起来像:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/navigation_drawer"
android:name="pl.ntimobile.personalcoach.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</LinearLayout>
我得到了,在我的logcat中:
10-01 11:13:53.325: E/AndroidRuntime(24113): FATAL EXCEPTION: main
10-01 11:13:53.325: E/AndroidRuntime(24113): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:910)
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.support.v4.widget.DrawerLayout.isDrawerOpen(DrawerLayout.java:1156)
10-01 11:13:53.325: E/AndroidRuntime(24113): at pl.company.appname.NavigationDrawerFragment.isDrawerOpen(NavigationDrawerFragment.java:79)
10-01 11:13:53.325: E/AndroidRuntime(24113): at pl.company.appname.FragmentAMain.onCreateOptionsMenu(FragmentAMain.java:135)
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.app.Activity.onCreatePanelMenu(Activity.java:2578)
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:224)
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:146)
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293)
10-01 11:13:53.325: E/AndroidRuntime(24113): at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:507)
10-01 11:13:53.325: E/AndroidRuntime(24113): at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:934)
10-01 11:13:53.325: E/AndroidRuntime(24113): at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:292)
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.os.Handler.handleCallback(Handler.java:730)
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.os.Handler.dispatchMessage(Handler.java:92)
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.os.Looper.loop(Looper.java:176)
10-01 11:13:53.325: E/AndroidRuntime(24113): at android.app.ActivityThread.main(ActivityThread.java:5419)
10-01 11:13:53.325: E/AndroidRuntime(24113): at java.lang.reflect.Method.invokeNative(Native Method)
10-01 11:13:53.325: E/AndroidRuntime(24113): at java.lang.reflect.Method.invoke(Method.java:525)
10-01 11:13:53.325: E/AndroidRuntime(24113): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
10-01 11:13:53.325: E/AndroidRuntime(24113): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
10-01 11:13:53.325: E/AndroidRuntime(24113): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
好的,明白了。我正在尝试(通过mystake)使用NavigationDrawerFragment将广告放入LinearLayout,而不是使用容器。在RelativeLayout中放置带容器的广告可以解决问题:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/adView" />
</RelativeLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="pl.ntimobile.personalcoach.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start" />
就像我想要的那样工作。
答案 1 :(得分:-2)
使用
import android.widget.LinearLayout
而不是
import android.support.v4.widget.LinearLayout