Android app crashes when using setVisibility(View.GONE);

0

I have a splash activity which runs for 5 seconds, and it I want 2 seconds to have a textview (txtCRF) and a imageview (canuck), and the other 3 seconds to have a different imageview (titlescreen). Whenever I utilize setVisibility(View.GONE) on any of the elements, the app crashes.

This is my code:

package com.example.bounce;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


public class Opening_Activity extends AppCompatActivity {


  private TextView txtCRF;
  private ImageView canuck;
  private ImageView titlescreen;
  public static final int VISIBLE = 0x00000000;
  public static final int INVISIBLE = 0x00000004;
  public static final int GONE = 0x00000008;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opening_);

    txtCRF = (TextView) findViewById(R.id.txtCRF);
    canuck = (ImageView) findViewById(R.id.canuck);
    titlescreen = (ImageView) findViewById(R.id.titlescreen);

    txtCRF.setVisibility(View.VISIBLE);
    canuck.setVisibility(View.VISIBLE);
    titlescreen.setVisibility(View.GONE);

    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                sleep(5000);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Intent mainIntent = new Intent(Opening_Activity.this, com.example.bounce.MainActivity.class);
                startActivity(mainIntent);
            }
        }
    };
    thread.start();

    Thread thread2 = new Thread() {
        @Override
        public void run() {
            try {
                sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                txtCRF.setVisibility(View.GONE);
                canuck.setVisibility(View.GONE);
                titlescreen.setVisibility(View.VISIBLE);
                MediaPlayer opening = MediaPlayer.create(Opening_Activity.this, R.raw.openingsound);
                opening.start();
            }
        }
    };
    thread2.start();

  }

  @Override
  protected void onPause() {
    super.onPause();

    finish();
  }
}

MY XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/background3"
   android:animateLayoutChanges="true"
   tools:context=".Opening_Activity">

   <ImageView
    android:id="@+id/canuck"
    android:layout_width="wrap_content"
    android:layout_height="200dp"
    android:src="@drawable/canuckyy"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="200dp"
    />

   <ImageView
    android:id="@+id/titlescreen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/background1" />

    <TextView
    android:id="@+id/txtCRF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="400dp"
    android:text="CRF"
    android:textColor="@color/white"
    android:textStyle="italic"
    android:textSize="27dp"/>

</RelativeLayout>
java
android
asked on Stack Overflow Feb 14, 2021 by user111 • edited Feb 14, 2021 by peprumo

1 Answer

0

Look at the logcat.

The thread is running in background and trying to change the ui and this throws an exception.

One possible fix is using runOnUithread from an activity context:

} finally {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            txtCRF.setVisibility(View.GONE);
            canuck.setVisibility(View.GONE);
            titlescreen.setVisibility(View.VISIBLE);
            MediaPlayer opening = MediaPlayer.create(Opening_Activity.this, R.raw.openingsound);
            opening.start();
        }
    });
}

Or you could use a handler.

answered on Stack Overflow Feb 14, 2021 by jeprubio • edited Feb 14, 2021 by jeprubio

User contributions licensed under CC BY-SA 3.0