微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

setVisibility 只能随机工作

如何解决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 语句被触发并且根本不会改变可见性。当第二次被触发时,它突然被触发。所以 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>

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

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

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,R.id.bottomNavigationView,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,0);
        } else {
            constraintSet.connect(R.id.fragment,0);
        }
        constraintSet.applyTo(constraintLayout);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。