如何从点击的项目中获取文件?

How to get file from clicked item?

您好,我仍在尝试制作我的 wavPlayer。我的应用程序有包含 wav 文件的列表项。当我点击一个文件时,它应该被播放但没有播放。我在 OnListItemClick() 上尝试了一些方法来让它工作(我用双斜线标记它),但它仍然出错。大多数错误都是空指针。有人可以帮助我吗?

这是我的代码和日志猫

public class PlayercobaActivity extends ListActivity implements View.OnClickListener {

    private MediaCursorAdapter mediaAdapter = null;
    PlayAudio playTask;
    Button startRecordingButton, stopRecordingButton, startPlaybackButton,
            stopPlaybackButton;
    boolean isPlaying = false;
    int frequency = 11025;
    int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
    //File item ;
    String item;

   // File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Datarekaman/recording5.wav");
    //Cursor acursor;
    Cursor cursor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_playercoba);

        startPlaybackButton = (Button) this .findViewById(R.id.StartPlaybackButton);
        stopPlaybackButton = (Button) this.findViewById(R.id.StopPlaybackButton);

        startPlaybackButton.setOnClickListener(this);
        stopPlaybackButton.setOnClickListener(this);

        startPlaybackButton.setEnabled(true);
        stopPlaybackButton.setEnabled(false);

        ContentResolver cr = getContentResolver();
        Uri uri = MediaStore.Files.getContentUri("external");
        String[] projection = null;
        String sortOrder = null; // unordered
        String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("wav");
        String[] selectionArgsPdf = new String[]{ mimeType };
        Cursor cursor = cr.query(uri, projection , selectionMimeType, selectionArgsPdf, sortOrder );

        if(null != cursor)
        {
            cursor.moveToFirst();

            mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);

            setListAdapter(mediaAdapter);


        }
    }

    //@Override
    public void onListItemClick(ListView list, View view, int position, long id) {
        super.onListItemClick(list, view, position, id);


        //String name = cursor.getString(cursor. getColumnIndex(MediaStore. MediaColumns.DISPLAY_NAME));
        //item =(String) list.getItemAtPosition(position);
        //File item = new File(name);
        //item = new File();
        //item = (String) view.getTag();
        //item = (String) getListAdapter().getItem(position);
        item = cursor.getString(cursor. getColumnIndex(MediaStore. MediaColumns.DATA));
        //playTask = new PlayAudio();
        //playTask.execute();
        Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
    }

    public void onClick(View v) {
        if (v == startPlaybackButton) {
            play();
        } else if (v == stopPlaybackButton) {
            stopPlaying();
        }
    }

    public void play() {
        startPlaybackButton.setEnabled(false);
        playTask = new PlayAudio();
        playTask.execute();
        stopPlaybackButton.setEnabled(true);
    }

    public void stopPlaying() {
        isPlaying = false;
        stopPlaybackButton.setEnabled(false);
        startPlaybackButton.setEnabled(true);
    }

    private class PlayAudio extends AsyncTask<Void, Integer, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            isPlaying = true;
            int bufferSize = AudioTrack.getMinBufferSize(frequency,
                    channelConfiguration, audioEncoding);
            short[] audiodata = new short[bufferSize/4];
            try {
                DataInputStream dis = new DataInputStream(
                        new BufferedInputStream(new FileInputStream(item)));
                AudioTrack audioTrack = new AudioTrack(
                        AudioManager.STREAM_MUSIC, frequency,
                        channelConfiguration, audioEncoding, bufferSize,
                        AudioTrack.MODE_STREAM);
                audioTrack.play();
                while (isPlaying && dis.available() > 0) {
                    int i = 0;
                    while (dis.available() > 0 && i < audiodata.length) {
                        audiodata[i] = dis.readShort();
                        i++;
                    }
                    audioTrack.write(audiodata, 0, audiodata.length);
                }
                dis.close();
                startPlaybackButton.setEnabled(false);
                stopPlaybackButton.setEnabled(true);
            } catch (Throwable t) {
                Log.e("AudioTrack", "Playback Failed");
            }
            return null;
        }

        protected void onPostExecute(Void result) {
            startPlaybackButton.setEnabled(true);
            stopPlaybackButton.setEnabled(false);
        }

    }

    private class MediaCursorAdapter extends SimpleCursorAdapter {

        public MediaCursorAdapter(Context context, int layout, Cursor c) {
            super(context, layout, c,
                    new String[] { MediaStore.MediaColumns.DISPLAY_NAME},
                    new int[] { R.id.displayname});
        }
    }
}

LogCat

05-19 23:01:17.015  13752-13752/com.example.agita.playercoba E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.agita.playercoba.PlayercobaActivity.onListItemClick(PlayercobaActivity.java:93)
            at android.app.ListActivity.onItemClick(ListActivity.java:319)
            at android.widget.AdapterView.performItemClick(AdapterView.java:301)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1287)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3078)
            at android.widget.AbsListView.run(AbsListView.java:4159)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4947)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)

如果你扩展CursorAdapter,你可以通过onListItemClick

的第一个参数获取光标所在位置
Cursor cursor = (Cursor) list.getItemAtPosition(position);
item = cursor.getString(cursor. getColumnIndex(MediaStore. MediaColumns.DATA));
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();