2013-11-19 1 views
1
<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> 

Это класс JavaClassCastException: AdView ошибочно получить отлиты к EditText

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); 

Ths генерирует следующую ошибку

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

Вы используете Eclipse? Попробуйте очистить проект и повторите попытку. Время от времени Eclipse наполняет иды. – Szymon

+0

Кроме того, для каждой переменной вы должны иметь только один символ '@ + id'. – Szymon

+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> 
+0

Хорошо, позвольте мне еще раз проверить – Szymon

+0

нет, Это была моя ошибка. Сейчас я тестирую –

+1

Это работает. Благодаря! –

 Смежные вопросы

  • Нет связанных вопросов^_^