【问题标题】:Why define an object in onActivityResult dosen't make sense?为什么在 onActivityResult 中定义一个对象没有意义?
【发布时间】:2017-08-19 11:01:15
【问题描述】:

我的片段中有四个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

我在这里做错了什么?

【问题讨论】:

  • 您必须在 onActivityResult 中定义和声明进度,即本地,您必须在 Fragment 的顶部定义进度全局,然后您才能全局访问它
  • @YogeshMane 我的代码有什么问题?
  • downvoter 你能解释一下为什么对我的问题投反对票吗?
  • 在 Fragment 之上声明 ProgressBar。它们超出了范围。
  • @MikeM。谢谢,你是对的,这似乎是我的观点问题,我认为图像涵盖了进展。我会检查的。

标签: java android android-progressbar onactivityresult


【解决方案1】:

好吧,谢谢@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 会受到这种行为的影响。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 嗯,谢谢,但现在可以了,请在下面查看我的答案。
    猜你喜欢
    • 1970-01-01
    • 2015-04-11
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    相关资源
    最近更新 更多