在Android中的VideoView中显示视频时出错

时间:2014-10-31 15:32:38

标签: android uri android-videoview

在尝试将视频设置为android对话框片段中的VideoView时,我收到空指针异常。这是我的logcat内容

 Caused by: java.lang.NullPointerException
        at com.shimba.android.ngoma.fragments.VideoUploadFragment.onCreateDialog(VideoUploadFragment.java:52)
        at android.support.v4.app.DialogFragment.getLayoutInflater(DialogFragment.java:307)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1133)
        at android.app.Activity.performStart(Activity.java:4475)
        at android.app.Activity.performRestart(Activity.java:4526)
        at android.app.Activity.performResume(Activity.java:4531)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2450)
        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2488)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1174)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4440)
       at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
                at dalvik.system.NativeStart.main(Native Method)

我的代码:

    public class VideoUploadFragment extends DialogFragment implements AdapterView.OnItemSelectedListener, View.OnClickListener {
    Spinner genres;
    VideoView videoView;
    TextView submit;
    String filePath;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.fragment_videoupload_dialog, null);
        builder.setView(v);

        genres = (Spinner) v.findViewById(R.id.videoupload_spinner_genre);
submit=(TextView)v.findViewById(R.id.videoupload_textView_submit);

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(),
                R.layout.ngoma_spinner, getResources().getStringArray(R.array.genres));
        dataAdapter
                .setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        genres.setAdapter(dataAdapter);


        Bundle bundle = getArguments();
        if(bundle != null)
        {
            filePath= bundle.getString("filePath");
        }
        Log.i("videopath", filePath);
        videoView.setVideoPath(filePath);

        genres.setOnItemSelectedListener(this);
        submit.setOnClickListener(this);
        return builder.create();
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.videoupload_textView_submit:
                getDialog().dismiss();
                break;
        }
    }
}

错误在行

videoView.setVideoPath(filePath);

但是,我可以看到

的价值
Log.i("videopath", filePath);

哪个是

10-31 18:23:16.382  13883-13883/ngoma.android.shimba.com.ngoma I/videopath﹕ /mnt/sdcard/Pictures/Ngoma/VID_20141031_182304.mp4

我在哪里失去轨道?

1 个答案:

答案 0 :(得分:0)

videoView中致电videoView.setVideoPath(filePath)之前,您忘了初始化onCreateView(),它应该是这样的:

videoView = (VideoView) v.findViewById(R.id.your_videoview_id);