Android 试图在VideoView中播放视频,但应用程序崩溃

Android 试图在VideoView中播放视频,但应用程序崩溃,android,Android,我是android开发领域的初学者,尝试在VideoView中播放视频,但当我运行应用程序时,它崩溃了 基本上,我想从我的存储器中运行视频,因此在创建了一个新的android资源目录“raw”后,我将视频文件放在res中 这是logcat文件 12-14 09:26:05.915 1332-1332/com.govinddixit.videoplayer E/AndroidRuntime: FATAL EXCEPTION: main

我是android开发领域的初学者,尝试在VideoView中播放视频,但当我运行应用程序时,它崩溃了 基本上,我想从我的存储器中运行视频,因此在创建了一个新的android资源目录“raw”后,我将视频文件放在res中

这是logcat文件

12-14 09:26:05.915 1332-1332/com.govinddixit.videoplayer E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.govinddixit.videoplayer/com.govinddixit.videoplayer.MainActivity}: java.lang.NullPointerException
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                                                                               at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                               at android.os.Looper.loop(Looper.java:137)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                               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:793)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                               at dalvik.system.NativeStart.main(Native Method)
                                                                            Caused by: java.lang.NullPointerException
                                                                               at com.govinddixit.videoplayer.MainActivity.onCreate(MainActivity.java:14)
                                                                               at android.app.Activity.performCreate(Activity.java:5104)
                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                               at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                               at android.os.Looper.loop(Looper.java:137) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                               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:793) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                               at dalvik.system.NativeStart.main(Native Method) 
12-14 09:26:05.923 457-684/system_process W/ActivityManager:   Force finishing activity com.govinddixit.videoplayer/.MainActivity
12-14 09:26:06.255 457-684/system_process D/dalvikvm: GC_FOR_ALLOC freed 1390K, 39% free 5482K/8880K, paused 11ms, total 12ms
12-14 09:26:06.295 457-473/system_process D/dalvikvm: GC_FOR_ALLOC freed 28K, 34% free 5948K/8880K, paused 17ms, total 18ms
12-14 09:26:06.295 457-473/system_process I/dalvikvm-heap: Grow heap (frag case) to 6.994MB for 1127532-byte allocation
12-14 09:26:06.327 457-472/system_process D/dalvikvm: GC_FOR_ALLOC freed 1K, 30% free 7048K/9984K, paused 30ms, total 30ms
12-14 09:26:06.347 457-459/system_process D/dalvikvm: GC_CONCURRENT freed 1K, 29% free 7149K/9984K, paused 2ms+5ms, total 18ms
12-14 09:26:06.775 457-470/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{5333415c u0 com.govinddixit.videoplayer/.MainActivity}
12-14 09:26:06.791 646-646/com.android.launcher W/EGL_genymotion: eglSurfaceAttrib not implemented
12-14 09:26:06.947 457-511/system_process I/qtaguid: Failed write_ctrl(s 0 10055) res=-1 errno=1
12-14 09:26:06.947 457-511/system_process W/NetworkManagementSocketTagger: setKernelCountSet(10055, 0) failed with errno -1
12-14 09:26:08.095 1332-1332/com.govinddixit.videoplayer I/Process: Sending signal. PID: 1332 SIG: 9
12-14 09:26:08.099 457-608/system_process I/ActivityManager: Process com.govinddixit.videoplayer (pid 1332) has died.
12-14 09:26:08.127 167-216/? I/AudioMixer: found effect "Multichannel Downmix To Stereo" from The Android Open Source Project
12-14 09:26:08.235 457-468/system_process W/InputMethodManagerService: Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@5336caa0 attribute=null, token = android.os.BinderProxy@532c82f4
12-14 09:26:36.279 640-643/com.android.phone D/dalvikvm: GC_CONCURRENT freed 361K, 15% free 2884K/3368K, paused 0ms+0ms, total 6ms
12-14 09:29:01.343 457-632/system_process W/ThrottleService: unable to find stats for iface rmnet0
12-14 09:29:04.855 53-53/? W/Genyd: Can't parse request
12-14 09:29:04.891 53-53/? D/Genyd: Received Set Clipboard
12-14 09:29:04.891 53-53/? D/Genymotion: Received Set Clipboard
12-14 09:29:17.851 53-53/? D/Genyd: Received Set Clipboard
12-14 09:29:17.851 53-53/? D/Genymotion: Received Set Clipboard
下面是Java的主要活动

package com.govinddixit.videoplayer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.net.Uri;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        VideoView videoView = (VideoView) findViewById(R.id.videoView);;
        videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.hall));
        videoView.start();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
这是XML文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.govinddixit.videoplayer.MainActivity">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="570dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>


非常感谢您的帮助

您需要在
setContentView()之后初始化控件
如下代码

 package com.govinddixit.videoplayer;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.net.Uri;
    import android.widget.VideoView;

    public class MainActivity extends AppCompatActivity {
        @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                VideoView videoView = (VideoView) findViewById(R.id.videoView);
                videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.hall));
                videoView.start();


            }
   }

首先删除一个
setContentView
应首先始终在setContentView(R.layout.activity_main)之后初始化代码;谢谢你,兄弟great@GOVINDDIXIT很高兴帮助你