如果将Button放在视图上方,则为ClassCastException

时间:2013-07-25 08:26:00

标签: java android android-layout button relativelayout

将按钮放在View中创建的分隔线上方时遇到问题。在下面的代码中,我可以放置按钮和文本,如图所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <LinearLayout
        android:id="@+id/image_card_wrapper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/image_card"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:baselineAlignBottom="false"
            android:contentDescription="@string/image_card"
            android:src="@drawable/image10herz" />
    </LinearLayout>

    <View
        android:id="@+id/separator_card"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/image_card_wrapper"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/text_card_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/separator_card"
        android:layout_marginRight="16dp"
        android:text="test1"
        android:textSize="14sp"
        android:textStyle="bold" />

    <View
        android:id="@+id/separator_card_info"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@+id/text_card_info"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray" />

    <Button
        android:id="@+id/button_next_card"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/separator_card_info"
        android:text="@string/button_next_card"
        android:textSize="12sp" />

    <Button
        android:id="@+id/button_klo_card"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/separator_card_info"
        android:layout_toLeftOf="@+id/button_next_card"
        android:text="@string/button_klo_card"
        android:textSize="12sp"
        android:visibility="visible" />

    <Button
        android:id="@+id/button_king"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/separator_card_info"
        android:layout_toLeftOf="@+id/button_klo_card"
        android:text="@string/button_king"
        android:textSize="12sp"
        android:visibility="visible" />

    <ToggleButton
        android:id="@+id/button_music"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/separator_copyright_play"
        android:textOff="@string/button_music_off"
        android:textOn="@string/button_music_on"
        android:textSize="12sp"
        android:visibility="visible" />

    <View
        android:id="@+id/separator_copyright_play"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_above="@+id/text_copyright_play"
        android:layout_marginBottom="16dp"
        android:layout_marginTop="16dp"
        android:background="@android:color/darker_gray" />

    <TextView
        android:id="@+id/text_copyright_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_alignParentBottom="true"
        android:text="@string/text_copyright" />

</RelativeLayout>

这是布局图片:

enter image description here

如您所见,我想显示此水平线上方的所有按钮以及android:layout_toLeftOf="..."一行中的所有按钮。每次执行android:layout_above="@+id/horizontal_line_bottom"并启动活动时,我都会收到以下ClassCastException

05-28 16:48:12.624: E/AndroidRuntime(4260): FATAL EXCEPTION: main
05-28 16:48:12.624: E/AndroidRuntime(4260): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mho.circleofdeathapp/com.mho.circleofdeathapp.PlayActivity}: java.lang.ClassCastException: android.view.View cannot be cast to android.widget.ToggleButton
05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.os.Looper.loop(Looper.java:137)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at java.lang.reflect.Method.invokeNative(Native Method)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at java.lang.reflect.Method.invoke(Method.java:511)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at dalvik.system.NativeStart.main(Native Method)
05-28 16:48:12.624: E/AndroidRuntime(4260): Caused by: java.lang.ClassCastException: android.view.View cannot be cast to android.widget.ToggleButton
05-28 16:48:12.624: E/AndroidRuntime(4260):     at com.mho.circleofdeathapp.PlayActivity.onCreate(PlayActivity.java:69)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.Activity.performCreate(Activity.java:5104)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-28 16:48:12.624: E/AndroidRuntime(4260):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-28 16:48:12.624: E/AndroidRuntime(4260):     ... 11 more

在我的onCreate()方法中,我只按右侧按钮(ToggleButtonButton)投射视图。如果我不更改布局XML中的设置,这一切都正常,但如果我这样做,如果我启动Activity,我会得到相应的异常。

编辑:添加onCreate()方法。

 @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);
    // Show the Up button in the action bar.
    setupActionBar();

    nextCardButton = (Button) findViewById(R.id.button_next_card);
    nextCardButton.setOnClickListener(this);

    kloCardButton = (Button) findViewById(R.id.button_klo_card);
    kloCardButton.setOnClickListener(this);

    kingButton = (Button) findViewById(R.id.button_king);
    kingButton.setOnClickListener(this);

    soundButton = (ToggleButton) findViewById(R.id.button_music);
    soundButton.setOnClickListener(this);

    imageView = (ImageView) findViewById(R.id.image_card);
    imageView.setImageBitmap(getImage(R.drawable.bier_head));

    cardTextView = (TextView) findViewById(R.id.text_card_info);

    initialize();
  }

1 个答案:

答案 0 :(得分:0)

好的,我发现解决方案可能有问题,但我不知道为什么这有效。对于每个按钮,我删除了标记android:visibility="visible",现在它正在运行。