我是android的新手,一直在学习,现在我找不到答案。我的问题是为什么我崩溃了。我知道我的问题在于:Music m = (Music) lv.getItemAtPosition(position);。我可以将其注释掉并替换为字符串ex: string song_name = "mp3";一切都很顺利。
我想做的是制作一个歌曲列表,在手机上任何地方都可以找到,并在选择更改按钮的文本设置为选择的标题。就像我说的,除了当我去使用Music m = (Music) lv.getItemAtPosition(position);
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
ListView listView = getListView();
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION};
List<String> songs = new ArrayList<String>();
ArrayAdapter<String> arrayAdaptor = new ArrayAdapter<String>(MusicListActivity.this, R.layout.a_listview,R.id.tv, songs);
listView.setAdapter(arrayAdaptor);
Cursor cursor = this.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection,null, null);
while(cursor.moveToNext()){
songs.add(cursor.getString(1) + "||" +
cursor.getString(2));
cursor.getString(3);
Collections.sort(songs);
String sname = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
Music mb = new Music();
mb.setName(sname);
}
}
protected void onListItemClick(ListView lv, View v, int position, long id) {
super.onListItemClick(lv, v, position, id);
Music m = (Music) lv.getItemAtPosition(position);
String song_name = m.getName();
Intent i = getIntent();
String msg = i.getStringExtra("local");
if(msg.contentEquals("song")){
i.putExtra("songname", song_name);
setResult(RESULT_OK, i);
finish();
}
}}
这是我的日志,
04-18 06:23:12.932: W/dalvikvm(3383): threadid=1: thread exiting with uncaught exception (group=0x40017560)
04-18 06:23:12.932: E/AndroidRuntime(3383): FATAL EXCEPTION: main
04-18 06:23:12.932: E/AndroidRuntime(3383): java.lang.ClassCastException: java.lang.String
04-18 06:23:12.932: E/AndroidRuntime(3383): at com.alarm.smsalarmclock.MusicListActivity.onListItemClick(MusicListActivity.java:59)
04-18 06:23:12.932: E/AndroidRuntime(3383): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
04-18 06:23:12.932: E/AndroidRuntime(3383): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-18 06:23:12.932: E/AndroidRuntime(3383): at android.widget.ListView.performItemClick(ListView.java:3513)
04-18 06:23:12.932: E/AndroidRuntime(3383): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
04-18 06:23:12.932: E/AndroidRuntime(3383): at android.os.Handler.handleCallback(Handler.java:587)
04-18 06:23:12.932: E/AndroidRuntime(3383): at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 06:23:12.932: E/AndroidRuntime(3383): at android.os.Looper.loop(Looper.java:130)
04-18 06:23:12.932: E/AndroidRuntime(3383): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-18 06:23:12.932: E/AndroidRuntime(3383): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 06:23:12.932: E/AndroidRuntime(3383): at java.lang.reflect.Method.invoke(Method.java:507)
04-18 06:23:12.932: E/AndroidRuntime(3383): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
04-18 06:23:12.932: E/AndroidRuntime(3383): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
04-18 06:23:12.932: E/AndroidRuntime(3383): at dalvik.system.NativeStart.main(Native Method)
我试过用谷歌搜索FATAL EXCEPTION: main java.lang. classcastexception: java.lang. class。字符串和变化,但没有找到任何东西。如果你能给我指出正确的方向,那就太好了,谢谢。
您尝试将String
转换为Music
,但是您不能。如果您只需要歌名,您可以尝试:
String song_name = (String) lv.getItemAtPosition(position);
您的arrayAdapter
包含String
s,但您正在将它们转换为Music
:
Music m = (Music) lv.getItemAtPosition(position);
String song_name = m.getName();
更改为:
String song_name = (String)lv.getItemAtPosition(position);
尝试此错误是由于错误的数据类型转换
String song_name =lv.getItemAtPosition(position).getName()
提供给listview的数组是一个字符串数组,它基本上包含歌曲名称。直接从那里提取歌名,如果那是你感兴趣的对象。问题是您试图将字符串转换为音乐。
protected void onListItemClick(ListView lv, View v, int position, long id) {
super.onListItemClick(lv, v, position, id);
String song_name = lv.getItemAtPosition(position);
Intent i = getIntent();
String msg = i.getStringExtra("local");
if(msg.contentEquals("song")){
i.putExtra("songname", song_name);
setResult(RESULT_OK, i);
finish();
}