java - 当我尝试启动更改对象可见性的方法时,应用程序崩溃

标签 java android crash android-progressbar

这是我的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
}

关于java - 当我尝试启动更改对象可见性的方法时,应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34949097/

相关文章:

java - 为什么 thread.sleep 会使我的 JavaFX 应用程序崩溃?

android - 从具有两个单独 TextView 的 Listview 菜单项检索值

iphone - 设备中应用程序启动异常,在模拟器中运行良好

objective-c - 奇怪的崩溃,没有调试器线索

JAVA:了解调用静态方法的方法/类

java - 带有 com.google.android.gms.common.api.ApiException : 7508 的 Google Awareness API Places

android - 在android中单击时隐藏默认键盘

android - Android的Firebase:java.lang.NoClassDefFoundError:com.fasterxml.jackson.databind.ObjectMapper

java - 如何在 Spring MVC 中将所有新用户请求重定向到首页?

java - String.xml 包含太多项目,速度太慢