![](/img/trans.png)
[英]Android setVisibility(View.Visible) not working on a layout
[英]startB.setVisibility(View.VISIBLE) crashes on Android Studio
尝试使进度条达到最大值(100%)之后出现一个按钮,但由于某种原因应用程序崩溃。 我可以让它更改颜色并在进度条加载后使其不可见,但由于某种原因使其不可见,请帮忙!
这是class.java代码:
int progress = 0;
ProgressBar simpleProgressBar;
Button startB = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enteredring_page);
// initiate progress bar and start button
simpleProgressBar = (ProgressBar) findViewById(R.id.loadingfightbar);
setProgressValue(progress);
startB = (Button)findViewById(R.id.showfight);
// startB.setVisibility(View.VISIBLE);
View overlay = findViewById(R.id.inringlayout);
overlay.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
VideoView mVideoView = (VideoView) findViewById(R.id.videoView);
String uriPath = "android.resource://com.seth.boxingadventure.boxerapp/" + R.raw.hayahadvid;
Uri uri = Uri.parse(uriPath);
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();
}
private void setProgressValue(final int progress) {
// set the progress
simpleProgressBar.setProgress(progress);
// thread is used to change the progress value
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
if(simpleProgressBar.getProgress() == simpleProgressBar.getMax())
{
// startB.setVisibility(View.VISIBLE);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
setProgressValue(progress + 10);
}
});
thread.start();
}
}
和xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:id="@+id/inringlayout"
android:weightSum="1">
<VideoView
android:id="@+id/videoView"
android:layout_width="fill_parent"
android:layout_height="318dp"
android:layout_weight="0.87" />
<ProgressBar
android:id="@+id/loadingfightbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:angle="270"
android:centerColor="#C27452"
android:centerY="0.75"
android:endColor="#050505"
android:startColor="#F0500A"
android:visibility="visible"
tools:visibility="visible"
android:layout_below="@+id/videoView"
android:layout_centerHorizontal="true"
android:layout_marginTop="81dp"
android:max="100"
android:progress="50"
/>
<Button
android:id="@+id/showfight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:layout_marginBottom="17dp" />
在Android中,主线程(UI线程)只能访问UI组件,即,如果要从后台线程访问UI组件,则不能从非UI线程更改UI元素,请使用runOnUiThread。
试试下面的代码:
private void setProgressValue(final int progress) {
// set the progress
simpleProgressBar.setProgress(progress);
// thread is used to change the progress value
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
if(simpleProgressBar.getProgress() == simpleProgressBar.getMax())
{
runOnUiThread(new Runnable() {
@Override
public void run() {
startB.setVisibility(View.VISIBLE);
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
setProgressValue(progress + 10);
}
});
thread.start();
}
在android中,只有UI /主线程可以更新任何UI组件,所以您不能从后台线程更新UI,这将导致崩溃
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
if(simpleProgressBar.getProgress() == simpleProgressBar.getMax())
{
// startB.setVisibility(View.VISIBLE);
// ^^^^^^^^^^^^^^^^^^ crash
}
} catch (InterruptedException e) {
e.printStackTrace();
}
setProgressValue(progress + 10);
}
});
解决方案:使用AsynchTask
或Handler或runOnUiThread
使用runOnUiThread像这样:
private void setProgressValue(final int progress) {
// set the progress
simpleProgressBar.setProgress(progress);
// thread is used to change the progress value
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000);
if(simpleProgressBar.getProgress() == simpleProgressBar.getMax()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
startB.setVisibility(View.VISIBLE);
}
});
}
} catch (InterruptedException e) {
e.printStackTrace();
}
setProgressValue(progress + 10);
}
});
thread.start();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.