我是一个新手,正在尝试创建视频播放器。我遇到了空指针异常,并已阅读了SO上的所有问题,并搜索了与之相关的Google并阅读了。在我看来,thumbpath似乎不是null,尽管这可能是导致此异常的唯一原因。有人可以帮我吗?谢谢

SDVideos.java

package com.example.dell_1.myapp3;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class SDVideos extends Activity
{
    private Cursor videoCursor;
    private int videoColumnIndex;
    ListView videolist;
    int count;
    String thumbPath;

    String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,MediaStore.Video.Thumbnails.VIDEO_ID };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdvideos);
        initialization();
    }

    private void initialization()
    {
        System.gc();
        String[] videoProjection = { MediaStore.Video.Media._ID,MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.DISPLAY_NAME,MediaStore.Video.Media.SIZE };
        videoCursor =getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,videoProjection, null, null, null);
        count = videoCursor.getCount();
        videolist = (ListView) findViewById(R.id.PhoneVideoList);

        videolist.setAdapter(new VideoListAdapter(this.getApplicationContext()));
        videolist.setOnItemClickListener(videogridlistener);
    }

    private AdapterView.OnItemClickListener videogridlistener = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView parent, View v, int position, long id)
        {
            videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            videoCursor.moveToPosition(position);
            String filename = videoCursor.getString(videoColumnIndex);
            Log.i("FileName: ", filename);
//Intent intent = new Intent(VideoActivity.this, ViewVideo.class);
//intent.putExtra("videofilename", filename);
//startActivity(intent);
        }};

    public class VideoListAdapter extends BaseAdapter
    {
        private Context vContext;

        public VideoListAdapter(Context c)
        {
            vContext = c;
        }

        public int getCount()
        {
            return videoCursor.getCount();
        }

        public Object getItem(int position)
        {
            return position;
        }

        public long getItemId(int position)
        {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            View listItemRow;
            listItemRow = LayoutInflater.from(vContext).inflate(R.layout.listitem, parent, false);

            TextView txtTitle = (TextView)listItemRow.findViewById(R.id.txtTitle);
            TextView txtSize = (TextView)listItemRow.findViewById(R.id.txtSize);
            ImageView thumbImage = (ImageView)listItemRow.findViewById(R.id.imgIcon);

            videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
            videoCursor.moveToPosition(position);
            txtTitle.setText(videoCursor.getString(videoColumnIndex));

            videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
            videoCursor.moveToPosition(position);
            txtSize.setText(" Size(KB):" + videoCursor.getString(videoColumnIndex));

            int videoId = videoCursor.getInt(videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
            Cursor videoThumbnailCursor =getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                    thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID+ "=" + videoId, null, null);

            if (videoThumbnailCursor.moveToFirst())
            {
                thumbPath = videoThumbnailCursor.getString(videoThumbnailCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
                Log.i("ThumbPath: ",thumbPath);
            }
            thumbImage.setImageURI(Uri.parse(thumbPath));
            return listItemRow;
        }
    }
}


XML档案:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <ListView
        android:id="@+id/PhoneVideoList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="#242424"
        android:dividerHeight="1dp"/>
    </LinearLayout>


LOGCAT:

08-05 17:29:17.109 17708-17708/com.example.dell_1.myapp3 E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.dell_1.myapp3, PID: 17708
        java.lang.NullPointerException: uriString
        at android.net.Uri$StringUri.<init>(Uri.java:475)
    at android.net.Uri$StringUri.<init>(Uri.java)
    at android.net.Uri.parse(Uri.java:437)
    at com.example.dell_1.myapp3.SDVideos$VideoListAdapter.getView(SDVideos.java:112)
    at android.widget.AbsListView.obtainView(AbsListView.java:2367)
    at android.widget.ListView.makeAndAddView(ListView.java:1972)
    at android.widget.ListView.fillDown(ListView.java:704)
    at android.widget.ListView.fillFromTop(ListView.java:765)
    at android.widget.ListView.layoutChildren(ListView.java:1744)
    at android.widget.AbsListView.onLayout(AbsListView.java:2161)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5618)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5618)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5618)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5618)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
    at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
    at android.view.View.layout(View.java:17523)
    at android.view.ViewGroup.layout(ViewGroup.java:5618)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2374)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2101)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1278)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6357)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
    at android.view.Choreographer.doCallbacks(Choreographer.java:683)
    at android.view.Choreographer.doFrame(Choreographer.java:619)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

最佳答案

我看不到thumbPath需要成为SDVideos字段的任何原因,它仅在VideoListAdapter.getView内部使用。

将thumbPath作为变量移动的更新代码如下:

 int videoId = videoCursor.getInt(videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
        Cursor videoThumbnailCursor =getContentResolver().query(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID+ "=" + videoId, null, null);

        if (videoThumbnailCursor.moveToFirst())
        {
            String thumbPath = videoThumbnailCursor.getString(videoThumbnailCursor.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
            Log.i("ThumbPath: ",thumbPath);
            thumbImage.setImageURI(Uri.parse(thumbPath));
        }

        return listItemRow;


由于只有在光标具有任何数据时才会填充thumbPath,因此可以将setImageURI移动到if的内部。

这段代码将解决NullPointerException,但您可能不会在thumbImage中看到任何图像。

现在,您需要查找为什么videoThumbnailCursor不返回任何数据。找到原因后,请更新您的问题。

08-17 11:08