получение исключения приведения класса, вызванного ImageButton - PullRequest
1 голос
/ 06 марта 2012

У меня есть это приложение, которое работало, находило и внезапно зависало из-за ClassCastException: android.widget.ImageButton.Единственное изменение, которое я сделал между рабочим и нет, был фон относительного макета от сплошного цвета до рисованного (синий с рисунком).

xml файл:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="vertical" >

<include
    android:id="@+id/linearLayout1_ref"
    android:layout_width="match_parent"
    android:layout_height="75dip"
    layout="@layout/title_area" />


<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/fur" >

    <View
        android:id="@+id/view5"
        style="@style/BlueBandHor"
        android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <View
        android:id="@+id/view3"
        style="@style/BlueBandVer"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/view5"
        android:layout_centerVertical="true" />

    <View
        android:id="@+id/view4"
        style="@style/BlueBandVer"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/view5"
        android:layout_centerVertical="true" />

    <View
        android:id="@+id/view1"
        style="@style/BlueBandHor"
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="48dp" />

    <ImageButton
        android:id="@+id/btn1"
        style="@style/Image"
        android:layout_below="@id/view5"
        android:layout_centerHorizontal="true"
        android:contentDescription="@string/portrait"
        android:onClick="onButtonClick"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:src="@drawable/orionhead" 
        android:background="@drawable/button_active" 
        android:clickable="true"/>

    <ImageButton
        android:id="@+id/btn3"
        style="@style/Image"
        android:layout_toRightOf="@+id/view3"
        android:contentDescription="@string/portrait"
        android:onClick="onButtonClick"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:src="@drawable/stonewallhead" 
        android:background="@drawable/button_active" 
        android:clickable="true" 
        android:layout_centerVertical="true"/>

    <Button
        android:id="@+id/how_tobtn"
        style="@style/HowTo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/view1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dip"
        android:background="@drawable/howto_button_active"
        android:padding="5dip"
        android:text="@string/How_to"
        android:textColor="@color/yellow"
        android:textSize="25dip" 
        android:onClick="onButtonClick"/>

    <ImageButton
        android:id="@+id/btn2"
        style="@style/Image"
        android:layout_toLeftOf="@+id/view4"
        android:contentDescription="@string/portrait"
        android:onClick="onButtonClick"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:src="@drawable/truffelhead" 
        android:background="@drawable/button_active" 
        android:clickable="true" 
        android:layout_centerVertical="true"/>

</RelativeLayout>

Java-файл:

 public class ImageActivity extends Activity {

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

public void onClick(View view) 
{Intent intent;
    switch (view.getId()) {
    case R.id.how_tobtn:
        intent = new Intent(this, Howbtn.class);
        break;
    case R.id.btn1:
        intent = new Intent(this, Btn1.class);
        break;
    case R.id.btn2:
        intent = new Intent(this, Btn2.class);
        break;
    default:
    intent = new Intent(this, Btn3.class);
    break;
            }   

Есть идеи, в чем проблема?Спасибо за любую помощь.

1 Ответ

1 голос
/ 06 марта 2012

Я думаю, проблема в том, что где-то в вашем коде вы воспринимаете how_tobtn как ImageButton, тогда как на самом деле это нормальный Button.

<Button
        android:id="@+id/how_tobtn"
        style="@style/HowTo"
        android:layout_width="wrap_content"
...

Ошибка, вероятно, в какой-то строке, например:

(ImageButton)getView(R.id.how_tobtn);

Чтобы избежать этого, либо измените ваш XML или приведите его к правильному типу в коде:

(Button)getView(R.id.how_tobtn);

РЕДАКТИРОВАТЬ:

Я также пересмотреть название моей деятельности. Класс имена типа Btn, HowtoBtn могут сбивать с толку, особенно для вас.

...