Moving adMob ads to the bottom of Relative Layout crashes the app

时间:2023-01-22 15:46:47

I'm implementing adMob ads to my app and I have a small problem with it. When I add the AdMob ads to my app and they're automatically positioned on the top of the layout it all works good. But when I try to move the ad down to the bottom of the relative layout the app crashes.

我正在向我的应用实施adMob广告,但我遇到了一个小问题。当我将AdMob广告添加到我的应用中并且它们自动定位在布局的顶部时,一切正常。但是,当我尝试将广告向下移动到相对布局的底部时,应用程序崩溃了。

Logcat:

08-14 13:57:53.126: E/AndroidRuntime(4141): FATAL EXCEPTION: main
08-14 13:57:53.126: E/AndroidRuntime(4141): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whizzappseasyvoicenotepad/com.whizzappseasyvoicenotepad.TabLayout}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whizzappseasyvoicenotepad/com.whizzappseasyvoicenotepad.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Chronometer
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.os.Looper.loop(Looper.java:137)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at java.lang.reflect.Method.invoke(Method.java:525)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at dalvik.system.NativeStart.main(Native Method)
08-14 13:57:53.126: E/AndroidRuntime(4141): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whizzappseasyvoicenotepad/com.whizzappseasyvoicenotepad.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Chronometer
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2054)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:749)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.widget.TabHost.setCurrentTab(TabHost.java:413)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.widget.TabHost.addTab(TabHost.java:240)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at com.whizzappseasyvoicenotepad.TabLayout.onCreate(TabLayout.java:33)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.Activity.performCreate(Activity.java:5133)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-14 13:57:53.126: E/AndroidRuntime(4141):     ... 11 more
08-14 13:57:53.126: E/AndroidRuntime(4141): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Chronometer
08-14 13:57:53.126: E/AndroidRuntime(4141):     at com.whizzappseasyvoicenotepad.MainActivity.onCreate(MainActivity.java:71)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.Activity.performCreate(Activity.java:5133)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-14 13:57:53.126: E/AndroidRuntime(4141):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-14 13:57:53.126: E/AndroidRuntime(4141):     ... 21 more

I tested it, and everytime I implement ads, they work until I move them down to the bottom of the relative layout. What could be the problem? I can see from the logcat that there's a problem with ToggleButton so I even tried moving the togglebutton around the layout but it doesn't fix anything.

我对它进行了测试,每次实施广告时,它们都会工作,直到我将它们移到相对布局的底部。可能是什么问题呢?我可以从logcat看到ToggleButton存在问题所以我甚至尝试在布局周围移动togglebutton但它没有修复任何东西。

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" xmlns:app="http://schemas.android.com/apk/lib/com.google.ads">

<Chronometer
    android:id="@+id/timer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/adView"
    android:layout_centerHorizontal="true"
    android:text="Chronometer" />

<ImageButton
    android:id="@+id/recButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/timer"
    android:layout_centerHorizontal="true"
    android:background="@null"
    android:onClick="recordBtnClick"
    android:src="@drawable/record_btn" />

<ToggleButton
    android:id="@+id/tBtn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:textOff="Touch to record"
    android:textOn="Touch to record" />

<com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:loadAdOnCreate="true"
    ads:testDevices="TEST_EMULATOR, 3F40A7AF85746C87"
    app:adSize="BANNER"
    app:adUnitId="a1520b668ee78d8" >
</com.google.ads.AdView>

2 个解决方案

#1


0  

You have a class cast exception at line 71 of your code. If line 71 of your code is

您在代码的第71行有一个类强制转换异常。如果代码的第71行是

  chTimer = (Chronometer)findViewById(R.id.timer);

as you indicated in your response to Arshad then you have a confused build environment where the generated resource ids are not what you think they are.

正如您在对Arshad的回复中所指出的那样,您有一个混乱的构建环境,其中生成的资源ID不是您认为的那样。

Do a clean build so that the resource ids are regenerated.

执行干净的构建,以便重新生成资源ID。

#2


0  

Please check your MainActivity.java file on Line No. 71. You are passing the Chronometer id into the ImageButton. That's why you are getting a Class cast exception

请检查第71行的MainActivity.java文件。您正在将计时器ID传递给ImageButton。这就是你获得Class强制异常的原因

#1


0  

You have a class cast exception at line 71 of your code. If line 71 of your code is

您在代码的第71行有一个类强制转换异常。如果代码的第71行是

  chTimer = (Chronometer)findViewById(R.id.timer);

as you indicated in your response to Arshad then you have a confused build environment where the generated resource ids are not what you think they are.

正如您在对Arshad的回复中所指出的那样,您有一个混乱的构建环境,其中生成的资源ID不是您认为的那样。

Do a clean build so that the resource ids are regenerated.

执行干净的构建,以便重新生成资源ID。

#2


0  

Please check your MainActivity.java file on Line No. 71. You are passing the Chronometer id into the ImageButton. That's why you are getting a Class cast exception

请检查第71行的MainActivity.java文件。您正在将计时器ID传递给ImageButton。这就是你获得Class强制异常的原因