Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果用户登录或未登录,如何检查元素之间的交换_Java_Android_Firebase_Firebase Authentication - Fatal编程技术网

Java 如果用户登录或未登录,如何检查元素之间的交换

Java 如果用户登录或未登录,如何检查元素之间的交换,java,android,firebase,firebase-authentication,Java,Android,Firebase,Firebase Authentication,这是我的导航标题: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="176dp" android:background="@color/colorAccent"

这是我的导航标题:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="176dp"
    android:background="@color/colorAccent"
    android:gravity="bottom"
    android:orientation="vertical"
    android:padding="16dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:text="John Doe"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="johndoe@gmail.com" />

</LinearLayout>
但我不知道如何/在何处设置条件以及如何更改条件。
1-登录时显示其照片/用户名/电子邮件
2-如果未登录,则显示登录/注册按钮/链接

当前代码(遵循Peter的回答)

应用程序崩溃导致以下错误:

尝试调用虚拟方法'void com.google.firebase.auth.FirebaseAuth.addAuthStateListener(com.google.firebase.auth.FirebaseAuth$AuthStateListener)


首先,您需要检查用户是否登录:

FirebaseUser currentUser = mAuth.getCurrentUser();  
 if (currentUser != null) { 
   //Logged in
  } else { 
   // NOT logged in
  }
如果他已登录,则要显示电子邮件/用户名/图片,您可以使用

为每个小部件添加一个id:

android:id="@+id/userProfilePic"
android:id="@+id/email"
android:id="@+id/userName"
然后做:

 userProfilePic.setVisibility(View.VISIBLE);
 email.setVisibility(View.VISIBLE);
 userName.setVisibility(View.VISIBLE);

你查过我的答案了吗?对不起,老兄。我在准备考试。我今天就试试。哪里是写后端代码的合适地方?主要活动?另外,我应该使用
getElementById
获取这些元素吗?在
onCreate()
函数之前或内部?另外,当我尝试
currentUser=mAuth.getCurrentUser()时,应用程序在启动时崩溃不知道为什么
mAuth=FirebaseAuth.getInstance()这必须在
mAuth.addAuthStateListener(mAuthListener)之上因为
mAuth
没有使用您编写的代码引用堆中的任何对象。正在工作!!但是当我执行
userEmail.setVisibility(View.GONE)时
视图。不可见
,但元素仍然可见。已经重建了project.userEmail.setVisibility(View.GONE)
userEmail
应该是编辑文本的id
android:id="@+id/userProfilePic"
android:id="@+id/email"
android:id="@+id/userName"
 userProfilePic.setVisibility(View.VISIBLE);
 email.setVisibility(View.VISIBLE);
 userName.setVisibility(View.VISIBLE);