当我尝试启动一种方法来更改对象的可见性时应用程序崩溃

Application crashes when i try to start a method to change the visibility of an object

这是我的 android 应用程序的代码:

XML:

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="los"
        android:id="@+id/button"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:onClick="run"/>

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="91dp"/>

主要Activity:

public class MainActivity extends AppCompatActivity {

     boolean visible = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void run()  {
        ProgressBar progressBar1 = (ProgressBar) findViewById(R.id.progressBar);
        visible = !visible;

        if (visible){
          progressBar1.setVisibility(View.VISIBLE);
        }
        if (!visible){
            progressBar1.setVisibility(View.INVISIBLE);
        }

    }
}

该应用应该在单击按钮时隐藏和显示进度条。但是,单击按钮时应用程序崩溃。

这里:

public void run()

android:onClick 方法必须接受一个 View 类型的参数,因此将其更改为:

public void run(View view)  {
  ...your code here
}