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