android.widget.Scrollview无法强制转换为android.widget.Linearlayout

时间:2013-12-23 16:22:31

标签: android xml android-layout scrollview android-linearlayout

我在main.xml文件中收到此错误:“android.widget.Scrollview无法转换为android.widget.Linearlayout” 我尝试运行项目时出现错误 这是我的main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->



<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.google.ads.AdView android:id="@+id/adView"
                         xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
                         android:layout_width="fill_parent"
                         android:layout_height="53dp"
                         android:layout_alignParentBottom="true"
                         ads:adUnitId="a14e77c91b43c50"
                         ads:adSize="BANNER"
                         ads:testing="false"
                         ads:loadAdOnCreate="true"/>

<ScrollView      
android:id="@+id/scroll" 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/adView"> 

<LinearLayout 
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<Button android:id="@+id/holaPlayerBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_button_yellow"
android:text="@string/hola"
android:textColor="#000000"
android:textStyle="bold"/>

<Button android:id="@+id/adiosPlayerBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/custom_button_yellow"
android:text="@string/adios"
android:textColor="#000000"
android:textStyle="bold"/>

<Button android:background="@drawable/custom_button_yellow" 
android:id="@+id/buenosdiasPlayerBtn"
android:layout_height="wrap_content"
android:layout_width="fill_parent" 
android:text="@string/buenosdias"
android:textColor="#000000"
android:textStyle="bold"/>
....
</LinearLayout>

</ScrollView>

</RelativeLayout>

与此错误相关联的java代码为:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Para LeadBolt... no está muy claro como van estos IDs
    ad = new AdController(this, "141625063");
    ad.loadStartAd("402593567", "989759922");
    AppTracker.startSession(this, "558524843");


    // Crear la adView
    adView = new AdView(this, AdSize.BANNER, "ca-app-pub-8829538089345362/9479506111");


    LinearLayout layout = (LinearLayout)findViewById(R.id.scroll);

    // Añadirle la adView
    layout.addView(adView);

    // Iniciar una solicitud genérica para cargarla con un anuncio
    adView.loadAd(new AdRequest());

我得到的LogCat是这样的:

12-24 11:57:58.532: D/(28836): bjarch:Create hashTable
12-24 11:57:58.556: I/Ads(28836): To get test ads on this device, call adRequest.addTestDevice("75936679897CC4349BD1D98DA4C9B155");
12-24 11:57:58.633: I/dalvikvm(28836): threadid=3: reacting to signal 3
12-24 11:57:58.741: I/dalvikvm(28836): Wrote stack traces to '/data/anr/traces.txt'
12-24 11:57:58.741: D/dalvikvm(28836): DexOpt: --- BEGIN 'ads1527252550.jar' (bootstrap=0) ---
12-24 11:57:58.896: D/AndroidRuntime(28836): Shutting down VM
12-24 11:57:58.897: W/dalvikvm(28836): threadid=1: thread exiting with uncaught exception (group=0x41a0e6f0)
12-24 11:57:58.900: E/AndroidRuntime(28836): FATAL EXCEPTION: main
12-24 11:57:58.900: E/AndroidRuntime(28836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spc.learn.spanish/com.spc.learn.spanish.AprenderSpanish}: java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.LinearLayout
 12-24 11:57:58.900: E/AndroidRuntime(28836):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2049)
 12-24 11:57:58.900: E/AndroidRuntime(28836):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2083)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at android.app.ActivityThread.access$600(ActivityThread.java:134)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at android.os.Looper.loop(Looper.java:137)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at android.app.ActivityThread.main(ActivityThread.java:4697)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at java.lang.reflect.Method.invokeNative(Native Method)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at java.lang.reflect.Method.invoke(Method.java:511)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at dalvik.system.NativeStart.main(Native Method)
12-24 11:57:58.900: E/AndroidRuntime(28836): Caused by: java.lang.ClassCastException: android.widget.ScrollView cannot be cast to android.widget.LinearLayout
12-24 11:57:58.900: E/AndroidRuntime(28836):    at com.spc.learn.spanish.AprenderSpanish.onCreate(AprenderSpanish.java:144)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at android.app.Activity.performCreate(Activity.java:4542)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-24 11:57:58.900: E/AndroidRuntime(28836):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2013)
12-24 11:57:58.900: E/AndroidRuntime(28836):    ... 11 more
12-24 11:57:59.055: D/dalvikvm(28836): DexOpt: --- END 'ads1527252550.jar' (success) ---
12-24 11:57:59.055: D/dalvikvm(28836): DEX prep '/data/data/com.spc.learn.spanish/cache/ads1527252550.jar': unzip in 0ms, rewrite 314ms
12-24 11:57:59.135: D/CallStack(28836): #00  pc 002d637a  /system/lib/libwebcore.so
12-24 11:57:59.135: D/CallStack(28836): #01  pc 00557e76  /system/lib/libwebcore.so
12-24 11:57:59.135: D/CallStack(28836): #02  pc 0002d200  /system/lib/libdvm.so
12-24 11:57:59.184: I/dalvikvm(28836): threadid=3: reacting to signal 3
12-24 11:57:59.189: I/dalvikvm(28836): Wrote stack traces to '/data/anr/traces.txt'

我一直在尝试多次尝试删除此错误,但没办法。

4 个答案:

答案 0 :(得分:2)

问题在于您的xml文件 滚动 是您的ScrollView的ID。

在您的编码中,您使用此 android:id =“@ + id / scroll” 找到了LinearLayout的ID。所以有一个问题。所以改变:

LinearLayout layout = (LinearLayout) findViewById(R.id.scroll);

ScrollView layout = (ScrollView) findViewById(R.id.scroll);

如果您想将动态视图添加到LinearLayout然后为其提供id,则可能:

喜欢 android:id =“@ + id / linear

然后找到

LinearLayout layout = (LinearLayout) findViewById(R.id.linear);

谢谢!

答案 1 :(得分:0)

您正在尝试输入

  

ScrollView

  

的LinearLayout

代码中的以下行

LinearLayout layout = (LinearLayout) findViewById(R.id.scroll);

请使用

更正
ScrollView layout = (ScrollView) findViewById(R.id.scroll);

希望这有帮助。

答案 2 :(得分:0)

如果你想要你的LinearLayout,给它一个id(android:id="@+id/mylinearlayout"),然后用LinearLayout layout = (LinearLayout) findViewById(R.id.mylinearlayout);

抓住它

答案 3 :(得分:0)

最后,我通过xml结合AdMob解决了这个问题,如下所述: https://developers.google.com/mobile-ads-sdk/docs/android/banner_xml?hl=es

我删除了对

的调用
 layout.addView(adView);

adView.loadAd(new AdRequest());

解决了它!