gpt4 book ai didi

java - onActivityResult 中的 View 为空

转载 作者:行者123 更新时间:2023-11-30 12:02:04 27 4
gpt4 key购买 nike

由于某些原因,我的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com