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

时间:2015-06-16 09:46:05

标签: java android android-asynctask nullpointerexception android-videoview

我在我的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;
    }


}

}

当我运行App时,我得到一个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

2 个答案:

答案 0 :(得分:3)

您的VideoView永远不会被初始化。取消注释

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

答案 1 :(得分:1)

您尚未初始化VideoView对象 vd

vd = (VideoView )rootview.findViewById(R.id.videoView1);
相关问题