【发布时间】: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