2014-04-28 24 views
0

我是新來的android.This是代碼,我在getView()下面提到的異常。我試圖列出音樂標題名稱以及點擊時開始播放音樂的音樂持續時間。get arrayindexoutofboundException裏面getView

MainActivity.java

public class MainActivity extends Activity { 

    Cursor c; 
    int index; 
    int count; 
    MediaPlayer mp; 

    private ArrayList<String> mnamedata = new ArrayList<String>(); 
    private ArrayList<String> mdurationdata = new ArrayList<String>(); 

    public static class ViewHolder { 
     TextView mname; 
     TextView mduration; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     String cols[] = { MediaStore.Audio.Media.DISPLAY_NAME, 
       MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.DATA }; 

     try { 
      ContentResolver contentResolver = getContentResolver(); 
      Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
      c = contentResolver.query(uri, cols, null, null, null); 

      count = c.getCount(); 
      ListView lv = (ListView) findViewById(R.id.listView1); 
      lv.setAdapter(new MusicAdapter(getApplicationContext())); 
      mp = new MediaPlayer(); 

      for (int i = 0; i < c.getCount(); i++) { 
       mnamedata.add(c.getString(0)); 
       mdurationdata.add(c.getString(1)); 
      } 

      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> arg0, View arg1, 
         int arg2, long arg3) { 
        // TODO Auto-generated method stub 
        c.moveToPosition(arg2); 
        String fn = c.getString(2); 
        try { 
         if (mp.isPlaying()) 
          mp.reset(); 
         mp.setDataSource(fn); 
         mp.prepare(); 
         mp.start(); 

        } catch (Exception e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
      }); 
     } catch (Exception e) { 
      System.out.println("No sdcard presents"); 
     } 

    } 

    private class MusicAdapter extends BaseAdapter { 

     Context music_context; 
     LayoutInflater inflater; 

     private MusicAdapter(Context c) { 
      music_context = c; 
      inflater = (LayoutInflater) music_context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 

     @Override 
     public int getCount() { 
      // TODO Auto-generated method stub 
      return count; 
     } 

     @Override 
     public Object getItem(int position) { 
      // TODO Auto-generated method stub 
      return position; 
     } 

     @Override 
     public long getItemId(int position) { 
      // TODO Auto-generated method stub 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 
      ViewHolder holder; 
      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.listinflate, null); 
       holder = new ViewHolder(); 

       convertView.setTag(holder); 

      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 
      holder.mname = (TextView) convertView 
        .findViewById(R.id.mname); 
      holder.mduration = (TextView) convertView 
        .findViewById(R.id.mduration); 
      holder.mname.setText(mnamedata.get(position)); 
      holder.mduration.setText(mdurationdata.get(position)); 

      return convertView; 
     } 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

listinflate.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/mname" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginTop="10dp" 
     android:layout_weight="1" 
     android:clickable="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:text="Name" 
     android:textSize="6pt" 
     android:textStyle="bold" /> 

    <TextView 
     android:id="@+id/mduration" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
     android:clickable="false" 
     android:focusable="false" 
     android:focusableInTouchMode="false" 
     android:text="Code" 
     android:textSize="6pt" /> 

</LinearLayout> 

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    > 

</ListView> 

</RelativeLayout> 

輸出

04-29 01:02:38.455: E/AndroidRuntime(17156): FATAL EXCEPTION: main 
04-29 01:02:38.455: E/AndroidRuntime(17156): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 
04-29 01:02:38.455: E/AndroidRuntime(17156): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 
04-29 01:02:38.455: E/AndroidRuntime(17156): at java.util.ArrayList.get(ArrayList.java:304) 
04-29 01:02:38.455: E/AndroidRuntime(17156): at com.example.musiclistview.MainActivity$MusicAdapter.getView(MainActivity.java:132) 
04-29 01:02:38.455: E/AndroidRuntime(17156): at android.widget.AbsListView.obtainView(AbsListView.java:2603) 
04-29 01:02:38.455: E/AndroidRuntime(17156): at android.widget.ListView.measureHeightOfChildren(ListView.java:1253) 
+0

你可以發佈logcat的完整輸出嗎? – AmmarCSE

+0

在這行後面'c = contentResolver.query(uri,cols,null,null,null);''你必須做'c.moveToFirst();'因爲你實際上指向了第一條記錄之前。 –

+0

@BobMalooga相同的音樂標題和持續時間在整個列表視圖中重複 – user2533604

回答

0

變化的代碼,以確保光標添加c.moveToFirst()是在第一位置的對,並且爲了添加c.moveToNext()之前將光標移動到下一個位置也可以改變你的一段時間(!c.isAfterLast()){}來移動光標

... 

count = c.getCount(); 
ListView lv = (ListView) findViewById(R.id.listView1); 
lv.setAdapter(new MusicAdapter(getApplicationContext())); 
mp = new MediaPlayer(); 
c.moveToFirst(); 
for (int i = 0; i < c.getCount(); i++) { 
... 
c.moveToNext(); 
} 

...