我是android的新手。这是我在getView()里面得到异常的代码。我正在尝试列出音乐titlename以及音乐持续时间,点击后开始播放音乐。
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 :(得分:0)
更改代码以添加c.moveToFirst()以确保光标位于for之前的第一个位置并添加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();
}
...