<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:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Talk" >
<RelativeLayout
android:id="@+id/controlPanelLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" >
<ImageView
android:id="@+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<ImageView
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/playBtn"
android:layout_marginLeft="15dp"
android:src="@drawable/stop_simple" />
<ImageView
android:id="@+id/clearBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/stopBtn"
android:src="@drawable/clear_simple" />
<ImageView
android:id="@+id/speakerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/clearBtn"
android:layout_marginLeft="15dp"
android:src="@drawable/speaker_simple" />
<ImageView
android:id="@+id/settingsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/settings_simple" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="@+id/adTalk"
android:layout_below="@+id/controlPanelLayout"
android:layout_marginTop="10dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" >
<EditText
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:linksClickable="true" >
<requestFocus />
</EditText>
</LinearLayout>
<com.google.ads.AdView
xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/adTalk"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
googleads:adSize="BANNER"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
googleads:adUnitId="xxxxxxxxxxxx"
/>
</RelativeLayout>
这是Java类
private EditText textField;
private AdView addView;
textField = (EditText)findViewById(R.id.textField);
//Load ads
addView = (AdView)findViewById(R.id.adTalk);
AdRequest adRequest = new AdRequest();
adRequest.addKeyword("android games");
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
addView.loadAd(adRequest);
这会产生以下错误
11-19 10:57:11.998: E/AndroidRuntime(1196): FATAL EXCEPTION: main
11-19 10:57:11.998: E/AndroidRuntime(1196): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x.xxx/com.x.xxx.Talk}: java.lang.ClassCastException: com.google.ads.AdView cannot be cast to android.widget.EditText
11-19 10:57:11.998: E/AndroidRuntime(1196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
11-19 10:57:11.998: E/AndroidRuntime(1196): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-19 10:57:11.998: E/AndroidRuntime(1196): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-19 10:57:11.998: E/AndroidRuntime(1196): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-19 10:57:11.998: E/AndroidRuntime(1196): at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 10:57:11.998: E/AndroidRuntime(1196): at android.os.Looper.loop(Looper.java:137)
11-19 10:57:11.998: E/AndroidRuntime(1196): at android.app.ActivityThread.main(ActivityThread.java:5041)
11-19 10:57:11.998: E/AndroidRuntime(1196): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 10:57:11.998: E/AndroidRuntime(1196): at java.lang.reflect.Method.invoke(Method.java:511)
11-19 10:57:11.998: E/AndroidRuntime(1196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-19 10:57:11.998: E/AndroidRuntime(1196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-19 10:57:11.998: E/AndroidRuntime(1196): at dalvik.system.NativeStart.main(Native Method)
11-19 10:57:11.998: E/AndroidRuntime(1196): Caused by: java.lang.ClassCastException: com.google.ads.AdView cannot be cast to android.widget.EditText
11-19 10:57:11.998: E/AndroidRuntime(1196): at x.xxx.xxxx.Talk.onCreate(Talk.java:107)
11-19 10:57:11.998: E/AndroidRuntime(1196): at android.app.Activity.performCreate(Activity.java:5104)
11-19 10:57:11.998: E/AndroidRuntime(1196): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-19 10:57:11.998: E/AndroidRuntime(1196): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-19 10:57:11.998: E/AndroidRuntime(1196): ... 11 more
要解决此问题的难点是,如果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:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Talk" >
<RelativeLayout
android:id="@+id/controlPanelLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" >
<ImageView
android:id="@+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<ImageView
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/playBtn"
android:layout_marginLeft="15dp"
android:src="@drawable/stop_simple" />
<ImageView
android:id="@+id/clearBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/stopBtn"
android:src="@drawable/clear_simple" />
<ImageView
android:id="@+id/speakerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/clearBtn"
android:layout_marginLeft="15dp"
android:src="@drawable/speaker_simple" />
<ImageView
android:id="@+id/settingsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/settings_simple" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:layout_below="@+id/controlPanelLayout"
android:layout_marginTop="10dp"
>
<EditText
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:linksClickable="true"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
>
<requestFocus />
</EditText>
</LinearLayout>
<com.google.ads.AdView
xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/adTalk"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
googleads:adSize="BANNER"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
googleads:adUnitId="xxxxxxxxxx"
/>
</RelativeLayout>
为什么会这样?
答案 0 :(得分:2)
将您的布局更改为以下。确保您声明的每个变量只有一个@+id
(第一次使用它时)。
<RelativeLayout
android:id="@+id/controlPanelLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" >
<ImageView
android:id="@+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
<ImageView
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/playBtn"
android:layout_marginLeft="15dp"
android:src="@drawable/stop_simple" />
<ImageView
android:id="@+id/clearBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@id/stopBtn"
android:src="@drawable/clear_simple" />
<ImageView
android:id="@+id/speakerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/clearBtn"
android:layout_marginLeft="15dp"
android:src="@drawable/speaker_simple" />
<ImageView
android:id="@+id/settingsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/settings_simple" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_above="@+id/adTalk"
android:layout_below="@id/controlPanelLayout"
android:layout_marginTop="10dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" >
<EditText
android:id="@+id/textField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:linksClickable="true" >
<requestFocus />
</EditText>
</LinearLayout>
<com.google.ads.AdView
xmlns:googleads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@id/adTalk"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
googleads:adSize="BANNER"
android:layout_alignParentBottom="true"
android:layout_marginTop="5dp"
googleads:adUnitId="xxxxxxxxxxxx"
/>
</RelativeLayout>