Question

I'm new to android been learning as I go, and now at a point where I can't find an answer. My question Why am i crashing. I know my problem lies at: Music m = (Music) lv.getItemAtPosition(position);. I can comment that out and substitute a string ex: String song_name = "mp3"; and everything works.

What I'm trying to do is make a list of songs found anywhere on the phone and on selection change the text of a button is set to the title of the selection. Like I said I got everything working except when I go to 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 i = getIntent();
String msg = i.getStringExtra("local");
if(msg.contentEquals("song")){

    i.putExtra("songname", song_name);
    setResult(RESULT_OK, i);
    finish();

}
}}

here's my log,

  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)

I've tried googling FATAL EXCEPTION: main java.lang.ClassCastException: java.lang.String and variations of but not finding anything. If I could even just get pointed in the right direction would be helpful, and thanks in advanced.

Was it helpful?

Solution

You try to cast String to Music, but you cant. If you need only song name, you can try:

String song_name = (String) lv.getItemAtPosition(position);

OTHER TIPS

Your arrayAdapter contains Strings but you are casting them to Music at:

Music m = (Music) lv.getItemAtPosition(position);
String song_name = m.getName();

Change to:

 String song_name = (String)lv.getItemAtPosition(position);

Try this error is due to wrong datatype casting

String song_name =lv.getItemAtPosition(position).getName()

The array fed to listview is a string array, which basically contains song names. So just directly extract the song name from there, if that is your object of interest. The problem was you were trying to cast String to Music.

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

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top