startB.setVisibility(View.VISIBLE)在Android Studio上崩溃

时间:2018-05-08 06:46:09

标签: java android visibility

尝试在进度条达到最大值(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" />

3 个答案:

答案 0 :(得分:2)

在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);


        }
    });

解决方案:使用AsynchTaskHandler or runOnUiThread

答案 1 :(得分:2)

在Android中,主线程(UI线程)只能访问UI组件,即如果要从后台线程使用runOnUiThread访问UI组件,则无法从非UI线程更改UI元素。   尝试以下代码:

    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();

    }

答案 2 :(得分:1)

使用runOnUiThread ,如:

n