Android:尝试通过 AsyncTask 加载 videoview 时出现 NullPointerException [重复]

Posted

技术标签:

【中文标题】Android:尝试通过 AsyncTask 加载 videoview 时出现 NullPointerException [重复]【英文标题】:Android: NullPointerException when trying to load videoview through AsyncTask [duplicate] 【发布时间】:2015-09-01 01:33:00 【问题描述】:

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

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

是的,真是个愚蠢的错误【参考方案2】:

你还没有初始化VideoView对象vd

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

【讨论】:

以上是关于Android:尝试通过 AsyncTask 加载 videoview 时出现 NullPointerException [重复]的主要内容,如果未能解决你的问题,请参考以下文章

在 onCreate() 期间通过 AsyncTask 加载数据

Android:使用 Asynctask 从 Web 加载图像

android 开发之AsyncTask

Android:在 AsyncTask 中传递字符串参数?

android asynctask怎么用

android listview 适配器 asynctask 图像加载