处理由getView()引起的NullPointerException

时间:2015-08-27 15:59:04

标签: java android android-fragments android-activity

我很困惑为什么我的应用程序崩溃,并且在此行收到NullPointerException:

request.url

片段的完整代码:

myVideoView = (VideoView) getView().findViewById(R.id.video_view);

记录详细信息:

public class FirstFragment extends Fragment {
    //used in VideoView
    private VideoView myVideoView;
    private int position = 0;
    private ProgressDialog progressDialog;
    private MediaController mediaControls;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        if (mediaControls == null) {
            mediaControls = new MediaController(getActivity());
        }
        //initialize the VideoView
        myVideoView = (VideoView) getView().findViewById(R.id.video_view);
        // create a progress bar while the video file is loading
        progressDialog = new ProgressDialog(getActivity());
        // set a title for the progress bar
        progressDialog.setTitle("IAWS");
        // set a message for the progress bar
        progressDialog.setMessage("Loading...");
        //set the progress bar not cancelable on users' touch
        progressDialog.setCancelable(false);
        // show the progress bar
        progressDialog.show();

        try {
            //set the media controller in the VideoView
            myVideoView.setMediaController(mediaControls);
            //set the uri of the video to be played
            myVideoView.setVideoURI(Uri.parse("android.resource://" + MainActivity.PACKAGE_NAME + "/" + R.raw.iaws));
        } catch (Exception e) {
            if(e.getMessage()!=null) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
        }
        myVideoView.requestFocus();
        //setOnPreparedListener in order to know when the video file is ready for playback
        myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mediaPlayer) {
                // close the progress bar and play the video
                progressDialog.dismiss();
                //if we have a position on savedInstanceState, the video playback should start from here
                myVideoView.seekTo(position);
                if (position == 0) {
                    //remove comments if you want video to start automatically
                    //myVideoView.start();
                } else {
                    //coming from a resumed activity, video playback will be paused
                    myVideoView.pause();
                }
            }
        });
        return (FrameLayout) inflater.inflate(R.layout.fragment_one, container, false);
    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        // Use onSaveInstanceState in order to store the video playback position for orientation change
        savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
        myVideoView.pause();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        position = savedInstanceState.getInt("Position");
        myVideoView.seekTo(position);
        }
}

提前感谢您的任何帮助。

1 个答案:

答案 0 :(得分:0)

您在getView()内呼叫onCreateView。但View返回的getView()只有在您最后onCreateView返回后才能使用。

使用

View view = inflater.inflate(R.layout.fragment_one, container, false);

您创建了View

替换

myVideoView = (VideoView) getView().findViewById(R.id.video_view);

myVideoView = (VideoView) view.findViewById(R.id.video_view);

你应该没事。