我有一个
UserCustomAdapter
必须按下其中一个用于启动媒体播放器,另一个用于停止它和文本视图
这是代码
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
public class UserCustomAdapter extends ArrayAdapter<User> {
Context context;
int layoutResourceId;
ArrayList<User> data = new ArrayList<User>();
public UserCustomAdapter(Context context, int layoutResourceId,
ArrayList<User> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.textName = (TextView) row.findViewById(R.id.textView1);
holder.btnpause = (Button) row.findViewById(R.id.button1);
holder.btnplay = (Button) row.findViewById(R.id.button2);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
User user = data.get(position);
holder.textName.setText(user.getName());
if (user.getName().equals("first")) {
final MediaPlayer first = MediaPlayer.create(context, R.raw.first);
holder.btnpause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
first.pause();
}
});
holder.btnplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
first.start();
Handler h = new Handler();
Runnable stopPlaybackRun = new Runnable() {
public void run(){
first.stop();
first.release();
}
};
h.postDelayed(stopPlaybackRun, first.getDuration());
}
});
}
return row;
}
static class UserHolder {
TextView textName;
Button btnpause;
Button btnplay;
}
}
它第一次运行良好但第二次点击btnplay
时强制关闭
当我清理first.release();它第二次不播放声音,但也没有力量关闭!!!
我正在使用此自定义列表视图
http://www.androidhub4you.com/2013/02/muftitouch-listview-multi-click.html
这是日志
09-25 20:01:42.099: E/AndroidRuntime(9565): FATAL EXCEPTION: main
09-25 20:01:42.099: E/AndroidRuntime(9565): java.lang.IllegalStateException
09-25 20:01:42.099: E/AndroidRuntime(9565): at android.media.MediaPlayer._start(Native Method)
09-25 20:01:42.099: E/AndroidRuntime(9565): at android.media.MediaPlayer.start(MediaPlayer.java:1025)
09-25 20:01:42.099: E/AndroidRuntime(9565): at mr.mokaleme.torki.UserCustomAdapter$2.onClick(UserCustomAdapter.java:60)
09-25 20:01:42.099: E/AndroidRuntime(9565): at android.view.View.performClick(View.java:4204)
09-25 20:01:42.099: E/AndroidRuntime(9565): at android.view.View$PerformClick.run(View.java:17359)
09-25 20:01:42.099: E/AndroidRuntime(9565): at android.os.Handler.handleCallback(Handler.java:725)
09-25 20:01:42.099: E/AndroidRuntime(9565): at android.os.Handler.dispatchMessage(Handler.java:92)
09-25 20:01:42.099: E/AndroidRuntime(9565): at android.os.Looper.loop(Looper.java:137)
09-25 20:01:42.099: E/AndroidRuntime(9565): at android.app.ActivityThread.main(ActivityThread.java:5259)
09-25 20:01:42.099: E/AndroidRuntime(9565): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 20:01:42.099: E/AndroidRuntime(9565): at java.lang.reflect.Method.invoke(Method.java:511)
09-25 20:01:42.099: E/AndroidRuntime(9565): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
09-25 20:01:42.099: E/AndroidRuntime(9565): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
09-25 20:01:42.099: E/AndroidRuntime(9565): at dalvik.system.NativeStart.main(Native Method)
09-25 20:02:00.489: E/Trace(9609): error opening trace file: No such file or directory (2)