将adMob广告移至相对布局的底部会导致应用崩溃

时间:2013-08-14 12:34:18

标签: android xml eclipse

我正在将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

我对它进行了测试,每次实施广告时,它们都会工作,直到我将它们移到相对布局的底部。可能是什么问题呢?我可以从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 个答案:

答案 0 :(得分:0)

请检查第71行的MainActivity.java文件。您正在将计时器ID传递给ImageButton。这就是为什么你得到一个类强制转换异常

答案 1 :(得分:0)

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

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

正如您在对Arshad的回复中指出的那样,您的构建环境很混乱,而生成的资源ID并不是您认为的那样。

执行清理构建,以便重新生成资源ID。