【问题标题】:Null Object Reference within Firebase onDataChange Method after checking to ensure object is not null检查以确保对象不为空后,Firebase onDataChange 方法中的空对象引用
【发布时间】:2018-04-11 10:40:39
【问题描述】:

我正在尝试根据从 Firebase 数据库中检索到的值设置 TextView 的文本。我能够在方法中打印字符串,并且我添加了一个检查,如果 String 为空,则不设置文本,但检查已通过,当我调用 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>

我已确保引用有效且字符串已分配给用户名。我正在使用 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>

【问题讨论】:

标签: java android firebase firebase-realtime-database


【解决方案1】:

使用这个 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    相关资源
    最近更新 更多