我从类似的堆栈溢出尝试了以下内容 清洁,建设项目 重启Eclipse
我也尝试过更新所有东西:从SDK和Eclipse插件 重启模拟器等
Log 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");
} `
答案 0 :(得分: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之间切换时,这可能很痛苦。