Android:使用listview崩溃的片段

时间:2015-08-14 14:35:39

标签: android android-mediaplayer

我在片段中使用listview,其中我显示了许多声音(如声音库)。当用户点击任何项目时,我播放声音。但在随机播放数量后,应用程序崩溃了。

以下是片段的代码:

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$1.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$3.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)

1 个答案:

答案 0 :(得分:1)

来自docs if the internal player engine has not been initialized or has been released

您的mySound.release();方法导致IllegalStateException加注。

自完成以来资源被释放。尝试使用reset()代替release()方法,或者您必须创建Media Player,因为在release()之后,该对象不再可用