如何隐藏和显示 android 中的进度条?

How to hide and show the Progressbar in android?

我已经编写了这段代码来加载进度条并单击按钮使其不可见。 但是当我点击按钮时点击监听器不工作。我的应用显示 ANR。

这是我的代码: 布局:

   <ProgressBar
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignLeft="@+id/textView6"
    android:layout_alignStart="@+id/textView6"
    android:id="@+id/progressBar2" />

   <Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="stop progressbar"
    android:onClick="stopPB" />

和java文件;

public void stopPB(Bundle view) {
    super.onCreate(view);
    setContentView(R.layout.content_main);
    ProgressBar PB = (ProgressBar) findViewById(R.id.progressBar2);
    PB.setVisibility(View.GONE);
}

这样做:

public void stopPB(View view) {
ProgressBar PB = (ProgressBar) findViewById(R.id.progressBar2);
PB.setVisibility(View.GONE);
}