ImageButton无法添加到滑动菜单抽屉中

时间:2013-09-16 09:50:20

标签: android

  

我正在做一个小程序,因为我添加了一个按钮点击打开的抽屉,并且我能够显示文本,按钮,但我想在该抽屉中添加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无法强制转换为android.widget.ImageButton

1 个答案:

答案 0 :(得分: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);