简体   繁体   中英

startB.setVisibility(View.VISIBLE) crashes on Android Studio

Trying to make a button appear after the progress bar has reached max (100%) but the application crashes for some reason. I can make it change colour and make it invisible after the progress bar has loaded but not make it visible for some reason please help!

Here is the class.java code:

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

}

}

And the 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" />

In Android, The main thread(UI thread) can only access UI components ie you can not change UI elements from a non-UI thread, if you wanted to access UI components from the background threads use runOnUiThread.
try below code:

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

    }

In android, only UI/main thread can update any UI component so you cannot update UI from background thread, will result in crash

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


        }
    });

Solution : Use AsynchTask or Handler or runOnUiThread

Use runOnUiThread like :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM