AdView ClassCastException

  • Antworten:1
manni.tu
  • Forum-Beiträge: 3

09.04.2012, 19:52:56 via Website

Ich habe ein xml layout erstellt bei dem eine ScrollView den ganzen Inhalt enthält und eine AdMob AdView außerhalb von der Scroll View angezeigt wird, damit diese nicht immer mitscrollt. Nun Wollte ich diese AdView nach unten verlegen und bekomme immer eine ClassCastException, habe allerdings keine Ahnung warum. Jemand ne Idee?

Funktionierender Code:
1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 android:background="#C62919"
7 android:orientation="vertical" >
8
9 <com.google.ads.AdView
10 android:id="@+id/adView"
11 android:layout_width="wrap_content"
12 android:layout_height="wrap_content"
13 android:layout_gravity="center_horizontal"
14 ads:adSize="BANNER"
15 ads:adUnitId="?????????"
16 ads:loadAdOnCreate="true"
17 ads:testDevices="TEST_EMULATOR" />
18
19 <ScrollView
20 android:layout_width="fill_parent"
21 android:layout_height="match_parent"
22 android:background="#B40431">
23
24 <LinearLayout
25 android:layout_width="fill_parent"
26 android:layout_height="fill_parent"
27 android:orientation="vertical"
28 android:padding="5dp" >
29
30 <Button
31 android:id="@+id/button1"
32 android:layout_width="fill_parent"
33 android:layout_height="wrap_content"
34 android:onClick="onClickBack"
35 android:text="@string/Button_Zurueck" />
36
37 <TextView
38 android:id="@+id/textView1"
39 android:layout_width="wrap_content"
40 android:layout_height="wrap_content"
41 android:layout_gravity="center"
42 android:text=""
43 android:textSize="100dp"
44 android:typeface="serif" />
45
46
47
48 <TextView
49 android:id="@+id/textViewBeispielnamen"
50 android:layout_width="wrap_content"
51 android:layout_height="wrap_content"
52 android:layout_marginBottom="3dp"
53 android:text="@string/Beispielnamen"
54 android:textStyle="bold" />
55
56 <ImageView
57 android:layout_width="30dp"
58 android:layout_height="30dp"
59 android:layout_marginBottom="5dp"
60 android:src="@drawable/picture_femalesymbol" />
61
62 <TextView
63 android:id="@+id/textViewFemale"
64 android:layout_width="wrap_content"
65 android:layout_height="wrap_content"
66 android:layout_marginBottom="10dp"
67 android:text="" />
68
69 <ImageView
70 android:layout_width="30dp"
71 android:layout_height="30dp"
72 android:layout_marginBottom="5dp"
73 android:src="@drawable/picture_malesymbol" />
74
75 <TextView
76 android:id="@+id/textViewMale"
77 android:layout_width="wrap_content"
78 android:layout_height="wrap_content"
79 android:layout_marginBottom="10dp"
80 android:text="" />
81
82 <TextView
83 android:id="@+id/textViewText"
84 android:layout_width="wrap_content"
85 android:layout_height="wrap_content"
86 android:text="" />
87 </LinearLayout>
88 </ScrollView>
89
90</LinearLayout>

Code bei dem es zu einer ClassCastException kommt:

1<?xml version="1.0" encoding="utf-8"?>
2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 android:background="#C62919"
7 android:orientation="vertical" >
8
9 <ScrollView
10 android:layout_width="fill_parent"
11 android:layout_height="match_parent"
12 android:background="#B40431" >
13
14 <LinearLayout
15 android:layout_width="fill_parent"
16 android:layout_height="fill_parent"
17 android:orientation="vertical"
18 android:padding="5dp" >
19
20 <Button
21 android:id="@+id/button1"
22 android:layout_width="fill_parent"
23 android:layout_height="wrap_content"
24 android:onClick="onClickBack"
25 android:text="@string/Button_Zurueck" />
26
27 <TextView
28 android:id="@+id/textView1"
29 android:layout_width="wrap_content"
30 android:layout_height="wrap_content"
31 android:layout_gravity="center"
32 android:text=""
33 android:textSize="100dp"
34 android:typeface="serif" />
35
36 <TextView
37 android:id="@+id/textViewBeispielnamen"
38 android:layout_width="wrap_content"
39 android:layout_height="wrap_content"
40 android:layout_marginBottom="3dp"
41 android:text="@string/Beispielnamen"
42 android:textStyle="bold" />
43
44 <ImageView
45 android:layout_width="30dp"
46 android:layout_height="30dp"
47 android:layout_marginBottom="5dp"
48 android:src="@drawable/picture_femalesymbol" />
49
50 <TextView
51 android:id="@+id/textViewFemale"
52 android:layout_width="wrap_content"
53 android:layout_height="wrap_content"
54 android:layout_marginBottom="10dp"
55 android:text="" />
56
57 <ImageView
58 android:layout_width="30dp"
59 android:layout_height="30dp"
60 android:layout_marginBottom="5dp"
61 android:src="@drawable/picture_malesymbol" />
62
63 <TextView
64 android:id="@+id/textViewMale"
65 android:layout_width="wrap_content"
66 android:layout_height="wrap_content"
67 android:layout_marginBottom="10dp"
68 android:text="" />
69
70 <TextView
71 android:id="@+id/textViewText"
72 android:layout_width="wrap_content"
73 android:layout_height="wrap_content"
74 android:text="" />
75 </LinearLayout>
76 </ScrollView>
77
78 <com.google.ads.AdView
79 android:id="@+id/adView"
80 android:layout_width="wrap_content"
81 android:layout_height="wrap_content"
82 android:layout_gravity="center_horizontal"
83 ads:adSize="BANNER"
84 ads:adUnitId="????????"
85 ads:loadAdOnCreate="true"
86 ads:testDevices="TEST_EMULATOR" />
87
88</LinearLayout>

Antworten
Rafael K.
  • Forum-Beiträge: 2.359

10.04.2012, 08:19:12 via Website

1. Project -> Clean
Wenn man Die Reihenfolge von Elementen ändert, die mit +id neue IDs erzeugen, wird die R-Klasse häufig ungültig,
weil die Konstanten auf die falschen Objekte zeigen. Das könnte dazu führen, dass findViewById() nicht den View liefert, den man erwartet.

2. Wird die ClassCastException ja an einer bestimmten Stelle im Java Code auftreten.
Das wäre interessanter hier zu posten als das XML Layout.

Antworten