0

我正在做一个小程序,因为我添加了一个在单击按钮时打开的抽屉,并且我能够显示文本、按钮,但我想在该抽屉中添加 ImageButton,然后它会给出错误。

public class slidingDrawerDemo extends Activity implements OnClickListener {

ImageButton slideButton, b1, b2;
SlidingDrawer slidingDrawer;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    slideButton = (ImageButton) findViewById(R.id.slideButton);
    slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);


    b1 = (ImageButton) findViewById(R.id.imageButton1);
    b2 = (ImageButton) findViewById(R.id.imageButton2);

    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    OnClickListener(this);

    slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
        @Override
        public void onDrawerOpened() {
            slideButton.setBackgroundResource(R.drawable.closearrow);
        }
    });

    slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {
        @Override
        public void onDrawerClosed() {
            slideButton.setBackgroundResource(R.drawable.openarrow);
        }
    });
}

@Override
public void onClick(View v) {
    Button b = (Button) v;
    Toast.makeText(slidingDrawerDemo.this, b.getText() + " Clicked",
            Toast.LENGTH_SHORT).show();
}

}

activity_main.xml

activity_main.xml

     <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="bottom"
        android:orientation="vertical" >

        <SlidingDrawer
            android:id="@+id/SlidingDrawer"
            android:layout_width="500dp"
            android:layout_height="fill_parent"
            android:background="@layout/border"
            android:content="@+id/contentLayout"
            android:handle="@+id/slideButton"
            android:orientation="vertical"
            android:padding="5dip"
            android:scrollbarStyle="insideInset" >

            <Button
                android:id="@+id/slideButton"
                android:layout_width="158dp"
                android:layout_height="100dp"
                android:background="@drawable/closearrow" >
            </Button>

            <LinearLayout
                android:id="@+id/contentLayout"
                android:layout_width="fill_parent"
                android:layout_height="667dp"
                android:gravity="center"
                android:orientation="vertical"
                android:padding="5dip" >

                <ImageButton
                    android:id="@+id/imageButton2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/imagesb" />

                <ImageButton
                    android:id="@+id/imageButton1"
                    android:layout_width="405dp"
                    android:layout_height="200dp"
                    android:src="@drawable/imagesa" />

            </LinearLayout>

        </SlidingDrawer>

    </LinearLayout>

错误是 java.lang.ClassCastException: android.widget.Button cannot be cast to android.widget.ImageButton

4

1 回答 1

0

可能有两种可能:

1) 如果您在 xml 文件中使用 ImageButton 但尝试查找 Button 的 id:

2) 如果您在 xml 文件中使用 Button 但尝试查找 ImageButton 的 id :

如果您使用的是 ImageButton 然后设置 onCreate()

  ImageButton ib = (ImageButton)findViewById(R.id.imagebutton1);

如果您使用的是 Button 然后设置 onCreate()

  Button ib = (Button)findViewById(R.id.button1);

问题在这里:

 slideButton = (ImageButton) findViewById(R.id.slideButton);

您已经使用 ImageButton 进行了初始化,但是在您的 xml 文件中它是 Button..所以更改

 slideButton = (Button) findViewById(R.id.slideButton);
于 2013-09-16T10:05:02.810 回答