java - onActivityResult 中的 View 为空

标签 java android nullpointerexception

由于某些原因,我的 View 在 onActivityResult 中为 null。以下是我的实现方式:

public class Player extends AppCompatActivity {
    RelativeLayout selectVideo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        selectVideo = findViewById(R.id.selectVideo);
        selectVideo.setOnClickListener(view -> {
            Intent intent = new Intent(Player.this, SelectVideo.class);
            startActivityForResult(intent, 852);
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 852){
            if (resultCode == RESULT_OK) {
                if (data != null) {
                    // The view is null here
                    selectVideo.setVisibility(View.GONE);
                }
            }
        }
    }

}

SelectVideo 中,我将结果设置如下:

public void returnToPlayer(String path){
    Intent output = new Intent();
    output.putExtra("path", path);
    setResult(RESULT_OK, output);
    finish();
}

我无法重现这个,但我在 crashlytics 中得到了这个

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference

我知道我可以做到以下几点:(但我想知道为什么会这样)

if (selectVideo != null){
    selectVideo.setVisibility(View.GONE);
}

View 怎么可能为空?


编辑 1:

我怀疑这个问题与我的 xml 有关,但正如评论部分所要求的那样:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    tools:context=".Player">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/selectVideo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true">

            <ImageView
                android:id="@+id/addimg"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:background="@drawable/ab_add_video"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp"
                />

            <TextView
                android:layout_marginTop="10dp"
                android:id="@+id/addtxt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="SELECT A VIDEO"
                android:layout_below="@+id/addimg"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                android:layout_marginEnd="20dp"
                android:layout_marginStart="20dp"
                android:layout_marginBottom="20dp"/>

    </RelativeLayout>

</RelativeLayout>

最佳答案

There could only be one possible reason that it might not be working. The views get initialized again when onActivityResult is being called.

但是您可以尝试使用静态变量,然后尝试隐藏或显示 View (或)在 onActivityResult 中重新初始化并检查是否能解决您的问题。

虽然在看到这篇文章后我有疑问 here , 我确信这可能是它。

我认为您可能不必担心,除非它在设备模型中重复出现。此外,我不知道为什么会发生这种情况,因为这不是预期的行为,因为这可能是错误或可能是特定于设备的。

关于java - onActivityResult 中的 View 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58972881/

相关文章:

安卓平板电脑模拟器

android - 我如何在android中获取图像矩阵坐标?

java - 在扩展方法中获取 NullPointerException - Java

android - 如何列出android设备中的所有服务

java - 如何计算字符串数组中字符串的重复次数

java - 如何在java中使用正则表达式从多个文本文件中读取数据?

java - 需要根据分隔符分割字符串,但它们被分组

java - 处理 "java.lang.NullPointerException"

android - 捕获运行在 Android 上的 native 代码引发的异常

java - 实现 Google Play 游戏服务排行榜时缺少 BaseGameUtils\bin