setVisibility 仅随机工作

setVisibility only works randomly

我有一个非常奇怪的问题,setVisibility() 函数在代码的一部分工作,但在另一部分却不工作,即使它们完全相同。

这是我的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();

        setContentView(R.layout.activity_main);

        TextView verifyMsg = findViewById(R.id.verifyEmailMessage);

        ImageView bottomPlayer = findViewById(R.id.bottomPlayer);
        if((((ServiceState) this.getApplication()).isRunning())){
            bottomPlayer.setVisibility(View.VISIBLE);
        } else {
            bottomPlayer.setVisibility(View.INVISIBLE);
        }

        if(!fAuth.getCurrentUser().isEmailVerified()){
            verifyMsg.setVisibility(View.VISIBLE);
        ...

第一个 if 语句被触发并且根本不改变可见性。当第二次被触发时,它突然被触发。因此,Textviews 的可见性发生了变化,但 imageviews 没有。我是非常愚蠢还是我做错了什么?!顺便说一句,没有 logcat 错误。这也是我的 xml 文件:


<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main_activity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

     <TextView
             android:id="@+id/verifyEmailMessage"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_marginStart="8dp"
             android:layout_marginTop="8dp"
             android:layout_marginEnd="8dp"
             android:text="Bitte bestätigen sie die Email"
             android:textAlignment="center"
             android:textStyle="bold"
             android:visibility="gone"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent" />

     <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#111111"
        android:id="@+id/bottomPlayer"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView">

    </ImageView>

</androidx.constraintlayout.widget.ConstraintLayout>

我不知道该怎么办了。这个问题很蠢。

编辑:因为问题似乎出在其他地方:这是我的全部 class:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();

        setContentView(R.layout.activity_main);

        setWifiLock();

        BottomNavigationView bottomNavigationView = 
        findViewById(R.id.bottomNavigationView);
        NavController navController = Navigation.findNavController(this,  
        R.id.fragment);
        NavigationUI.setupWithNavController(bottomNavigationView, navController);

        createNotificationChanel();
        FirebaseAuth fAuth = FirebaseAuth.getInstance();

        //AlertDialog.Builder resetAlert = new AlertDialog.Builder(this);

        TextView verifyMsg = findViewById(R.id.verifyEmailMessage);
        Button verifyBtn = findViewById(R.id.verifyEmailBtn);

        ImageView bottomPlayer = findViewById(R.id.bottomPlayer);
        if((((ServiceState) this.getApplication()).isRunning())){
            bottomPlayer.setVisibility(View.VISIBLE);
        } else {
            bottomPlayer.setVisibility(View.INVISIBLE);
        }

        if(!fAuth.getCurrentUser().isEmailVerified()){

            verifyBtn.setVisibility(View.VISIBLE);
            verifyMsg.setVisibility(View.VISIBLE);

            NotificationCompat.Builder builder = new 
            NotificationCompat.Builder(MainActivity.this, "lemubitA")
             .setSmallIcon(R.drawable.ic_baseline_message_24)
             .setContentTitle("Bitte bestätige deine Email")
             .setContentText("- Schmitties Technik Freunde")
             .setPriority(NotificationCompat.PRIORITY_DEFAULT);

            NotificationManagerCompat notificationManager = 
            NotificationManagerCompat.from(this);
 
            notificationManager.notify(100, builder.build());

        }

        verifyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
 fAuth.getCurrentUser().sendEmailVerification().addOnSuccessListener(new 
 OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Toast.makeText(MainActivity.this, "Bestägungsemail wurde 
                        gesendet", Toast.LENGTH_SHORT).show();
                        verifyBtn.setVisibility(View.GONE);
                        verifyMsg.setVisibility(View.GONE);
                    }
                });
            }
        });

    }

    private void setWifiLock() {

        WifiManager.WifiLock wifilock = ((WifiManager) 
        getApplicationContext()
        .getSystemService(Context.WIFI_SERVICE))
        .createWifiLock(WifiManager.WIFI_MODE_FULL, "mylock");
        wifilock.acquire();

    }


    private void createNotificationChanel(){

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            CharSequence name = "BaywatchBerlin";
            String description = "Mit Klaas Heufer Umlauf";
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel notificationChannel = new 
            NotificationChannel("lemubitA", name, importance);
            notificationChannel.setDescription(description);
            notificationChannel.setSound(null, null);

            NotificationManager notificationManager = 
            getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(notificationChannel);
        }

    }

}

好的,我发现了问题:我没有在我的问题中提供它,因为我认为这无关紧要。这是 setVisibility 不起作用的代码

ConstraintLayout constraintLayout = findViewById(R.id.main_activity);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);

ImageView bottomPlayer = findViewById(R.id.bottomPlayer);
if((((ServiceState) this.getApplication()).isRunning())){
    bottomPlayer.setVisibility(View.VISIBLE);
    constraintSet.connect(R.id.fragment,ConstraintSet.BOTTOM,
    R.id.bottomPlayer,ConstraintSet.TOP,0);
} else {
    bottomPlayer.setVisibility(View.INVISIBLE);
    constraintSet.connect(R.id.fragment,ConstraintSet.BOTTOM,
    R.id.bottomNavigationView,ConstraintSet.TOP,0);
}
constraintSet.applyTo(constraintLayout);

这是它起作用的地方:

ImageView bottomPlayer = findViewById(R.id.bottomPlayer);
if((((ServiceState) this.getApplication()).isRunning())){
    bottomPlayer.setVisibility(View.VISIBLE);
} else {
    bottomPlayer.setVisibility(View.INVISIBLE);
}

ConstraintSet就是禁止setVisibility。有什么想法吗??

可能有更简单的方法,但我只是将这两个函数脱节,现在它工作正常:

ImageView bottomPlayer = findViewById(R.id.bottomPlayer);
        if((((ServiceState) this.getApplication()).isRunning())){
            bottomPlayer.setVisibility(View.VISIBLE);
        } else {
            bottomPlayer.setVisibility(View.INVISIBLE);
        }

        ConstraintLayout constraintLayout = findViewById(R.id.main_activity);
        ConstraintSet constraintSet = new ConstraintSet();
        constraintSet.clone(constraintLayout);
        if((((ServiceState) this.getApplication()).isRunning())){
            constraintSet.connect(R.id.fragment,ConstraintSet.BOTTOM,R.id.bottomPlayer,ConstraintSet.TOP,0);
        } else {
            constraintSet.connect(R.id.fragment,ConstraintSet.BOTTOM,R.id.bottomNavigationView,ConstraintSet.TOP,0);
        }
        constraintSet.applyTo(constraintLayout);