android - lv.getItemAtPosition(position) crash -
i'm new android been learning go, , @ point can't find answer. question why crashing. know problem lies at: music m = (music) lv.getitematposition(position);. can comment out , substitute string ex: string song_name = "mp3"; , works.
what i'm trying make list of songs found anywhere on phone , on selection change text of button set title of selection. said got working except when go use 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 = getintent(); string msg = i.getstringextra("local"); if(msg.contentequals("song")){ i.putextra("songname", song_name); setresult(result_ok, i); finish(); } }}
here's log,
04-18 06:23:12.932: w/dalvikvm(3383): threadid=1: thread exiting 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): @ com.alarm.smsalarmclock.musiclistactivity.onlistitemclick(musiclistactivity.java:59) 04-18 06:23:12.932: e/androidruntime(3383): @ android.app.listactivity$2.onitemclick(listactivity.java:319) 04-18 06:23:12.932: e/androidruntime(3383): @ android.widget.adapterview.performitemclick(adapterview.java:284) 04-18 06:23:12.932: e/androidruntime(3383): @ android.widget.listview.performitemclick(listview.java:3513) 04-18 06:23:12.932: e/androidruntime(3383): @ android.widget.abslistview$performclick.run(abslistview.java:1812) 04-18 06:23:12.932: e/androidruntime(3383): @ android.os.handler.handlecallback(handler.java:587) 04-18 06:23:12.932: e/androidruntime(3383): @ android.os.handler.dispatchmessage(handler.java:92) 04-18 06:23:12.932: e/androidruntime(3383): @ android.os.looper.loop(looper.java:130) 04-18 06:23:12.932: e/androidruntime(3383): @ android.app.activitythread.main(activitythread.java:3683) 04-18 06:23:12.932: e/androidruntime(3383): @ java.lang.reflect.method.invokenative(native method) 04-18 06:23:12.932: e/androidruntime(3383): @ java.lang.reflect.method.invoke(method.java:507) 04-18 06:23:12.932: e/androidruntime(3383): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:875) 04-18 06:23:12.932: e/androidruntime(3383): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:633) 04-18 06:23:12.932: e/androidruntime(3383): @ dalvik.system.nativestart.main(native method)
i've tried googling fatal exception: main java.lang.classcastexception: java.lang.string , variations of not finding anything. if pointed in right direction helpful, , in advanced.
you try cast string
music
, cant. if need song name, can try:
string song_name = (string) lv.getitematposition(position);
Comments
Post a Comment