图像按钮不能转换为小部件按钮 [英] Image Button cannot be cast to widget Button

查看:149
本文介绍了图像按钮不能转换为小部件按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从类似的堆栈溢出尝试了以下内容
清理,构建项目
重新启动Eclipse

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

我还尝试更新所有内容:从SDK和Eclipse插件
重新启动仿真器等

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

日志Cat输出:

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

代码

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");
}     `


推荐答案

ImageButton不是孩子的Button,不管名字是什么意思。请参阅
http://developer.android.com/reference/android/ widget / ImageButton.html
它扩展了ImageView,而不是Button。
其中一行

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);

导致此错误,因为视图是ImageButton而不是Button。实际上,由于您只将OnClickListeners分配给视图,因此似乎没有必要进行投射。 查看会做。这当然取决于你的其他代码,但是一般来说,如果不使用子方法,那么你一般不应该转换为子视图。特别是在按钮和ImageButtons之间进行切换时,可能会很痛苦。

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.

这篇关于图像按钮不能转换为小部件按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆