Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java findViewById()上的Android NullPointerException_Java_Android - Fatal编程技术网

Java findViewById()上的Android NullPointerException

Java findViewById()上的Android NullPointerException,java,android,Java,Android,我正在启动一个活动,并试图通过findviewbyd()获取视图(在本例中为视频视图) 在大多数情况下,这很好,但有时当我试图访问我先前试图查找的视图时,它会抛出NullPointerException 这就是我所从事的活动: public class Video extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onC

我正在启动一个活动,并试图通过
findviewbyd()获取视图(在本例中为视频视图)
在大多数情况下,这很好,但有时当我试图访问我先前试图查找的视图时,它会抛出NullPointerException

这就是我所从事的活动:

public class Video extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        Intent intent = getIntent();
        VideoView videoView = this.findViewById(R.id.VideoViewVideo);
        videoView.setVideoURI(someURI);
        videoView.start();
    }
}
异常在
videoView.setVideoURI(someURI)行中抛出并如下所示:

java.lang.RuntimeException: Unable to start activity ComponentInfo{Video}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoURI(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoUri(java.lang.String)' on a null object reference
at Video.onCreate(Video.java:29)
at android.app.Activity.performCreate(Activity.java:6687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
<?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="Video">

    <VideoView
        android:id="@+id/VideoViewVideo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
活动的布局文件如下所示:

java.lang.RuntimeException: Unable to start activity ComponentInfo{Video}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoURI(java.lang.String)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2678)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoUri(java.lang.String)' on a null object reference
at Video.onCreate(Video.java:29)
at android.app.Activity.performCreate(Activity.java:6687)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
<?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="Video">

    <VideoView
        android:id="@+id/VideoViewVideo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

我想我试图获取视图的方式可能有问题,但我不确定

如果有人能帮助我,我们将不胜感激。

请尝试移动此代码

VideoView videoView = this.findViewById(R.id.VideoViewVideo);
videoView.setVideoURI(someURI);
videoView.start();

有时,如果要创建视图,必须在onViewCreated而不是onCreate中定义它

让我知道它是否有效

您为什么要写作
VideoView-VideoView=this.findviewbyd(R.id.VideoViewVideo)
你可以不写上面的内容


VideoView VideoView=(VideoView)findViewById(R.id.VideoViewVideo)

也许您有特定于配置的布局文件夹和多个activity\u video.xml布局文件?如果是这样,请确保他们都有一个具有预期id的视频视图。不幸的是,这似乎不是问题所在。我没有任何其他名为activity_video.xml的文件。我唯一能想到的是,您有一些陈旧的构建工件。试着做一个干净的构建。@TedHopp干净的构建修复了它。是我做错了什么,还是这只是一个常见的Gradle/AndroidStudio问题?这种情况偶尔会发生。至于你是否做错了什么,我怀疑。可能是您在Gradle构建过程中编辑了源代码,而构建与自身不同步。字面上没有区别,知道了吗?在这里,我可以看到VideoView是一个自定义视图检查,如果它是从JavaCode初始化的,它不是自定义视图。问题中给出了xml。