为什么在 onActivityResult 中定义一个对象没有意义?

Why define an object in onActivityResult dosen't make sense?

我的片段中有四个 progress,我在 onActivityResult 中定义它们,例如:

 if (requestCode == GALLERY_IMAGE1 && resultCode == Activity.RESULT_OK) {
        ProgressBar progress = view.findViewById(R.id.progressBar1);
        final Uri imageUri = data.getData();
        InputStream imageStream = null;
        try {
            imageStream = getActivity().getContentResolver().openInputStream(imageUri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
        bitmap = selectedImage;
        image1.setImageBitmap(bitmap);
        Log.d(TAG, "onActivityResult: " + progress.getVisibility());
        editPhoto("imageName1", progress);
    }
    if (requestCode == GALLERY_IMAGE2 && resultCode == Activity.RESULT_OK) {
        ProgressBar progress = view.findViewById(R.id.progressBar2);
        /* bunch of code */
        editPhoto("imageName2", progress);
    }

在我的日志行中:Log.d(TAG, "onActivityResult: " + progress.getVisibility()); 我得到 0 个值,这意味着 GONE

当我尝试在我的 editPhoto 方法中使其可见时,例如:

private void editPhoto(final String imagePosition, final ProgressBar progress) {

        progress.setVisibility(View.VISIBLE);
        Log.d(TAG, "progress: " + progress.getVisibility());

/* bunch of code */

}

我的进度不可见,在我的日志行 Log.d(TAG, "progress: " + progress.getVisibility()); 我得到与上面相同的值 0

我做错了什么?

好吧,谢谢@MikeM,我的视图行为很糟糕,我在 frameLayout 中的进步如下:

<FrameLayout
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="left"
                android:layout_weight="1"
                android:gravity="left">

                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:id="@+id/progressBar4"
                    android:visibility="gone"/>
            <ImageView
                android:id="@+id/editImage4"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="right"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="5dp"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/ic_add_images" />

            </FrameLayout>

我将其更改为:

<FrameLayout
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="left"
                android:layout_weight="1"
                android:gravity="left">


            <ImageView
                android:id="@+id/editImage4"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="right"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="5dp"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/ic_add_images" />

                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:id="@+id/progressBar4"
                    android:visibility="gone"/>

            </FrameLayout>

我不知道 frameLayout 受此行为影响。

从 xml 中删除 android:visibility="gone" 行,并在 java 文件中以编程方式执行此操作。