Android:带有列表视图的片段崩溃
Android: Fragment with listview crashes
我在片段中使用列表视图,其中我显示了许多声音(如声音库)。当用户单击任何项目时,我会播放声音。但是在播放了随机次数后,应用程序崩溃了。
片段代码如下:
adapter= new soundAdapter(getActivity().getApplicationContext(), contact);
ListView myList = (ListView)view.findViewById(R.id.listView);
myList.setAdapter(adapter);
myList.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String clicked = String.valueOf(parent.getItemAtPosition(position));
SoundListed k = contact.get(position);
if(mySound!=null) {
if (mySound.isPlaying()) {
mySound.stop();
}
}
mySound = MediaPlayer.create(getActivity(), k.getImage());
mySound.start();
mySound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mySound.release();
}
});
}
});
SoundListed.class:
private int _id;
private String _productname;
private String _extra;
private int _image;
public SoundListed() {
}
public SoundListed(int id, String productname, String extra, int image) {
this._id = id;
this._productname = productname;
this._extra = extra;
this._image=image;
}
public SoundListed( String productname, String extra, int image) {
this._productname = productname;
this._extra = extra;
this._image=image;
}
public void setID(int id) {
this._id = id;
}
public int getID() {
return this._id;
}
public int getImage(){return this._image;}
public void setProductName(String productname) {
this._productname = productname;
}
public String getProductName() {
return this._productname;
}
public void setExtra(String extra) {
this._extra = extra;
}
public String getExtra() {
return this._extra;
}
这是我的 LogCat:
java.lang.IllegalStateException
at android.media.MediaPlayer.isPlaying(Native Method)
at com.inc.nicky.messengersayit.SoundFragment.onItemClick(SoundFragment.java:90)
at android.widget.AdapterView.performItemClick(AdapterView.java:308)
at android.widget.AbsListView.performItemClick(AbsListView.java:1524)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3531)
at android.widget.AbsListView.run(AbsListView.java:4898)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
正如the doc所说,isPlaying()
可以抛出IllegalStateException
if the internal player engine has not been initialized or has been released.
在 onCompletion
你调用 release()
-> isPlaying()
之后会抛出异常。
但是我们没有任何方法可以获取MediaPlayer的当前状态。
所以我认为 try/catch 的解决方法是唯一的解决方案:
if (mySound != null) {
boolean isPlay = false;
try {
isPlay = mySound.isPlaying();
} catch (IllegalStateException e) {
Log.d("tag", Log.getStackTraceString(e));
mySound.release();
}
if (isPlay) {
mySound.stop();
}
}
来自 docs if the internal player engine has not been initialized or has been released
您的 mySound.release();
方法导致 IllegalStateException
加注。
自完成资源释放。尝试使用 reset()
代替 release()
方法,或者您必须创建媒体播放器,因为在 release()
之后,对象不再可用
我在片段中使用列表视图,其中我显示了许多声音(如声音库)。当用户单击任何项目时,我会播放声音。但是在播放了随机次数后,应用程序崩溃了。
片段代码如下:
adapter= new soundAdapter(getActivity().getApplicationContext(), contact);
ListView myList = (ListView)view.findViewById(R.id.listView);
myList.setAdapter(adapter);
myList.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String clicked = String.valueOf(parent.getItemAtPosition(position));
SoundListed k = contact.get(position);
if(mySound!=null) {
if (mySound.isPlaying()) {
mySound.stop();
}
}
mySound = MediaPlayer.create(getActivity(), k.getImage());
mySound.start();
mySound.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mySound.release();
}
});
}
});
SoundListed.class:
private int _id;
private String _productname;
private String _extra;
private int _image;
public SoundListed() {
}
public SoundListed(int id, String productname, String extra, int image) {
this._id = id;
this._productname = productname;
this._extra = extra;
this._image=image;
}
public SoundListed( String productname, String extra, int image) {
this._productname = productname;
this._extra = extra;
this._image=image;
}
public void setID(int id) {
this._id = id;
}
public int getID() {
return this._id;
}
public int getImage(){return this._image;}
public void setProductName(String productname) {
this._productname = productname;
}
public String getProductName() {
return this._productname;
}
public void setExtra(String extra) {
this._extra = extra;
}
public String getExtra() {
return this._extra;
}
这是我的 LogCat:
java.lang.IllegalStateException
at android.media.MediaPlayer.isPlaying(Native Method)
at com.inc.nicky.messengersayit.SoundFragment.onItemClick(SoundFragment.java:90)
at android.widget.AdapterView.performItemClick(AdapterView.java:308)
at android.widget.AbsListView.performItemClick(AbsListView.java:1524)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3531)
at android.widget.AbsListView.run(AbsListView.java:4898)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5586)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
正如the doc所说,isPlaying()
可以抛出IllegalStateException
if the internal player engine has not been initialized or has been released.
在 onCompletion
你调用 release()
-> isPlaying()
之后会抛出异常。
但是我们没有任何方法可以获取MediaPlayer的当前状态。
所以我认为 try/catch 的解决方法是唯一的解决方案:
if (mySound != null) {
boolean isPlay = false;
try {
isPlay = mySound.isPlaying();
} catch (IllegalStateException e) {
Log.d("tag", Log.getStackTraceString(e));
mySound.release();
}
if (isPlay) {
mySound.stop();
}
}
来自 docs if the internal player engine has not been initialized or has been released
您的 mySound.release();
方法导致 IllegalStateException
加注。
自完成资源释放。尝试使用 reset()
代替 release()
方法,或者您必须创建媒体播放器,因为在 release()
之后,对象不再可用