setVisibility只能随机工作



我有一个非常奇怪的问题,其中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语句被触发,并且根本不改变可见性。当second When被触发,它突然被触发。textview可见性会改变,但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>

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

编辑:因为问题似乎在其他地方:这是我的整个班级:

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

最新更新