我是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>
非常感谢任何帮助
答案 0 :(得分:2)
您需要在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();
}
}