2

我从类似的堆栈溢出 Clean ,Build Project Restart Eclipse 中尝试了以下操作

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

日志猫输出:

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

1 回答 1

6

ImageButton 不是 Button 的子代,无论其名称如何。请参阅 http://developer.android.com/reference/android/widget/ImageButton.html 它扩展了 ImageView,而不是 Button。其中一条线

 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 分配给视图,因此似乎没有必要强制转换它们。“查看”就可以了。当然,这取决于您的代码的其余部分,但一般来说,如果您不使用子方法,则不应强制转换为子视图。尤其是在 Buttons 和 ImageButtons 之间切换时,这可能会很痛苦。

于 2013-09-26T22:34:15.683 回答