blended blended - 2 months ago 6
Java Question

Image Button cannot be cast to widget Button

I tried the following from similar stack overflow
Clean ,Build Project
Restart Eclipse

I also tried update everything : from SDK and Eclipse plugin
Restart emulator etc

Log Cat output:

09-26 22:11:49.182: E/AndroidRuntime(1393): FATAL EXCEPTION: main
09-26 22:11:49.182: E/AndroidRuntime(1393): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ass2/com.example.ass2.MP3Player}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
09-26 22:11:49.182: E/AndroidRuntime(1393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-26 22:11:49.182: E/AndroidRuntime(1393): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-26 22:11:49.182: E/AndroidRuntime(1393): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-26 22:11:49.182: E/AndroidRuntime(1393): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-26 22:11:49.182: E/AndroidRuntime(1393): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 22:11:49.182: E/AndroidRuntime(1393): at android.os.Looper.loop(Looper.java:137)
09-26 22:11:49.182: E/AndroidRuntime(1393): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-26 22:11:49.182: E/AndroidRuntime(1393): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 22:11:49.182: E/AndroidRuntime(1393): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 22:11:49.182: E/AndroidRuntime(1393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-26 22:11:49.182: E/AndroidRuntime(1393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-26 22:11:49.182: E/AndroidRuntime(1393): at dalvik.system.NativeStart.main(Native Method)
09-26 22:11:49.182: E/AndroidRuntime(1393): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
09-26 22:11:49.182: E/AndroidRuntime(1393): at com.example.ass2.MP3Player.onCreate(MP3Player.java:30)
09-26 22:11:49.182: E/AndroidRuntime(1393): at android.app.Activity.performCreate(Activity.java:5104)
09-26 22:11:49.182: E/AndroidRuntime(1393): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-26 22:11:49.182: E/AndroidRuntime(1393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-26 22:11:49.182: E/AndroidRuntime(1393): ... 11 more


Code

public class MP3Player extends Activity{

Button prev , play , stop , next , choosefiles;
ArrayAdapter<Song> adaptor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.mp3player);


//Bitmap mDummyAlbumArt = BitmapFactory.decodeResource(getResources(), R.drawable.dummy_album_art);


prev =(Button)findViewById(R.id.prev);
play =(Button)findViewById(R.id.play);
stop =(Button)findViewById(R.id.stop);
next =(Button)findViewById(R.id.next);
prev.setOnClickListener(new OnPress());
play.setOnClickListener(new OnPress());
stop.setOnClickListener(new OnPress());
next.setOnClickListener(new OnPress());

List<Song> playlist = new ArrayList<Song>();

System.out.println("Before adapter on creat");

Song test= new Song("sakis","rouvas","name","path");
playlist.add(test);

ListView list = (ListView) findViewById(R.id.listview);
adaptor = new ArrayAdapter<Song>(this,
R.layout.list_row, playlist);
list.setAdapter(adaptor);
registerForContextMenu(list);

System.out.println("after adapter on craet");
} `

Answer

ImageButton is not a child of Button, no matter what the name suggests. See http://developer.android.com/reference/android/widget/ImageButton.html It extends ImageView, not Button. One of the lines

 prev =(Button)findViewById(R.id.prev);
 play =(Button)findViewById(R.id.play);
 stop =(Button)findViewById(R.id.stop);
 next =(Button)findViewById(R.id.next);

causes this error because the view is an ImageButton, not a Button. In fact, since you only assign OnClickListeners to the views, casting them seems not necessary. "View" will do. This, of course, depends on the rest of your code but in general you should not cast to child views if you don't use the child methods. Especially for switching between Buttons and ImageButtons this can be a pain.

Comments