在导航抽屉中获取数据(Android) [英] Data fetching in navigation drawer (Android)
问题描述
我试图从firebase数据库中获取登录用户的名称,并试图将导航抽屉的标题放置在android的默认图像下方. 但是,当我尝试这样做时,应用程序崩溃且活动启动.我试过了 在活动中的简单textView中检索名称是可行的,但是当我尝试更新导航标题中的名称时,它将失败并崩溃.如何使这件事起作用.
I am trying to fetch the logged in user's name from firebase database and trying to place it the navigation drawer's header below the default image of android. But when I try to do so the app crashes and when the activity launches. I tried retriving name in simple textView in activity it works but when I try to update the name in navigation header it fails and crashes. How to do get this thing worked.
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = (String) dataSnapshot.child("users").child(userID).child("name").getValue();
outputName.setText(name);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是我提取名称的代码.请注意,如果我尝试吐司这个名称,则可以使用,但标头中的值未更新,通常会导致应用程序崩溃.
Here is my code for fetching name. Note that if I try to toast out the name it works but the value inside of header is not updating and it generally results in app crash.
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/userProfileImage"
android:layout_width="100dp"
app:civ_border_width="4dp"
app:civ_border_color="#fff"
android:layout_height="100dp"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:layout_centerHorizontal="true"
android:src="@drawable/profile_default_image"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="App User"
android:id="@+id/current"
android:layout_below="@+id/userProfileImage"
android:textAlignment="center"/>
这是我的xml代码.
我想用用户名更新圈出的区域.
I want to update the circled area with username.
推荐答案
由于您的textview为null,因此您可以像下面这样投射textview
It's due to your textview null you may to cast textview like below
View header=navigationView.getHeaderView(0);
outputName = (TextView)header.findViewById(R.id.current);
这篇关于在导航抽屉中获取数据(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!