Firebase onDataChange中的空对象引用检查以确保对象不为空

时间:2018-04-11 10:40:40

标签: java android firebase firebase-realtime-database

我尝试根据从Firebase数据库中检索到的值设置TextView的文本。我能够在方法中打印字符串,并且我添加了一个检查,如果String为null,则不设置文本,但检查通过,当我调用setText时,我得到一个空对象引用。

这是MapsActivity.java

private TextView headerText;  
private FirebaseAuth mAuth = FirebaseAuth.getInstance();
private FirebaseUser currentUser = mAuth.getCurrentUser();
private FirebaseDatabase database = FirebaseDatabase.getInstance();

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


    headerText = (TextView) findViewById(R.id.header_text);
    DatabaseReference ref = database.getReference("Users/"+currentUser.getUid()+"/user_name");
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String userName = (String) dataSnapshot.getValue();

            //debugging
            Toast.makeText(MapsActivity.this, userName, Toast.LENGTH_SHORT).show();

            //passes check and attempts to set
            if(userName!=null)
                headerText.setText(userName);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

这是nav_header.xml

  <?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="90dp"
      android:background="?attr/colorPrimary"
      android:padding="16dp"
      android:theme="@style/AppTheme"
      android:orientation="vertical"
      android:gravity="bottom">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/header_text">

</TextView>

我确保引用有效并且String被分配给userName。我使用onDataChange在项目中的其他各个位置设置值,并且在此之前没有遇到此问题

感谢您的帮助

编辑:为了完整性,请访问activity_maps.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#eaeaea"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="Map" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </fragment>

    </FrameLayout>

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/nav_menu"
    app:headerLayout="@layout/nav_header"/>

  </android.support.v4.widget.DrawerLayout>

1 个答案:

答案 0 :(得分:0)

使用它 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); View header = navigationView.getHeaderView(0) TextView headerText = (TextView) header.findViewById(R.id.header_text);

然后在TextView中设置如下

headerText.setText(userName);