Android:尝试通过 AsyncTask 加载 videoview 时出现 NullPointerException

Android: NullPointerException when trying to load videoview through AsyncTask

我在我的 android 项目中为我的 VideoView 创建了一个片段。我试图在后台任务中加载 VideoView 视频。

我的代码:

public class VideoPlayer extends Fragment {

//need these variable to be global within the class
MediaController mediaController;
VideoView vd;


public VideoPlayer() {
}




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    String link =  getActivity().getIntent().getExtras().getString("link");

    StartVideo(link);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootview =  inflater.inflate(R.layout.fragment_video_player, container, false);
    /*vd = (VideoView) rootview.findViewById(R.id.videoView);
    String link =  getActivity().getIntent().getExtras().getString("link");
    vd.setVideoURI(Uri.parse(link));
    MediaController mediaController = new MediaController(getActivity());
    mediaController.setAnchorView(vd);
    vd.setMediaController(mediaController);
    vd.requestFocus();

    vd.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        public void onPrepared(MediaPlayer arg0) {
            vd.start();
        }
    });*/
    return rootview;
}



@Override
public void onDetach() {
    super.onDetach();

}

public void StartVideo(String link){
    BackgrounfAsynctask bk = new BackgrounfAsynctask();
    bk.execute(link);
}

public class BackgrounfAsynctask extends AsyncTask<String,String,Void>{
    ProgressDialog dialog;

    protected void onPreExecute() {
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("Loading, Please Wait...");
        dialog.setCancelable(true);
        dialog.show();
    }

    protected void onProgressUpdate(final String... link) {

        try {

            mediaController = new MediaController(getActivity());
            Log.v("onProgressUpdate","uri "+link[0]);
            vd.setVideoURI(Uri.parse(link[0]));
            mediaController.setAnchorView(vd);
            vd.setMediaController(mediaController);

            //mediaController.show(10000);


            vd.requestFocus();
            vd.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                public void onPrepared(MediaPlayer arg0) {
                    dialog.dismiss();
                    vd.start();
                }
            });


        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    @Override
    protected Void doInBackground(String... params) {
        try {
            String[] link = params;
            publishProgress(link);
        } catch (Exception e) {
            e.printStackTrace();

        }

        return null;
    }


}

}

当我 运行 应用程序时,我收到 NullPointerException,因此我记录了变量以检查值,但值似乎是正确的。

异常:

06-16 14:58:54.254  28535-28535/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.aswin.movieapp, PID: 28535
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoURI(android.net.Uri)' on a null object reference
        at com.example.aswin.movieapp.VideoPlayer$BackgrounfAsynctask.onProgressUpdate(VideoPlayer.java:102)
        at com.example.aswin.movieapp.VideoPlayer$BackgrounfAsynctask.onProgressUpdate(VideoPlayer.java:86)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:656)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

日志:

06-16 14:58:54.253  28535-28535/? V/onProgressUpdate﹕ uri http://qthttp.apple.com.edgesuite.net/1010qwoeiuryfg/sl.m3u8

您的 VideoView 从未初始化。取消注释

vd = (VideoView) rootview.findViewById(R.id.videoView);

你还没有初始化 VideoView 对象 vd

vd = (VideoView )rootview.findViewById(R.id.videoView1);