1

我是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.String和变体但没有找到任何东西。如果我什至可以指出正确的方向会很有帮助,并在此先感谢。

4

4 回答 4

0

您的arrayAdaptercontains String,但您将它们投射到Music

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

改成:

 String song_name = (String)lv.getItemAtPosition(position);
于 2013-04-18T13:56:43.713 回答
0

您尝试投射StringMusic,但不能。如果您只需要歌曲名称,您可以尝试:

String song_name = (String) lv.getItemAtPosition(position);
于 2013-04-18T13:57:12.480 回答
0

试试这个错误是由于错误的数据类型转换

String song_name =lv.getItemAtPosition(position).getName()
于 2013-04-18T13:58:52.823 回答
0

提供给 listview 的数组是一个字符串数组,它基本上包含歌曲名称。因此,如果这是您感兴趣的对象,则直接从那里提取歌曲名称。问题是您试图将 String 转换为 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();

}
于 2013-04-18T14:00:30.013 回答