Android 可变livedata';在布局充气之前,将触发s observer

Android 可变livedata';在布局充气之前,将触发s observer,android,android-layout,android-architecture-components,android-livedata,Android,Android Layout,Android Architecture Components,Android Livedata,我有一个main活动,其中包含抽屉菜单的NavigationView。NavigationView的标题包含一个TextView,我想用一些用户信息填充它。 问题是,有时应用程序在onChanged方法中使用NullPointerException崩溃,因为textUsername是null。我猜那是因为布局还没有完全膨胀。我已经实现了null检查(如下面的代码所示),这显然只会导致textUsername在大多数情况下为空 我的onCreate方法如下所示: @Override protect

我有一个
main活动
,其中包含抽屉菜单的
NavigationView
NavigationView
的标题包含一个
TextView
,我想用一些用户信息填充它。 问题是,有时应用程序在
onChanged
方法中使用
NullPointerException
崩溃,因为
textUsername
null
。我猜那是因为布局还没有完全膨胀。我已经实现了
null
检查(如下面的代码所示),这显然只会导致
textUsername
在大多数情况下为空

我的
onCreate
方法如下所示:

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

    mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);

    mainDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
    mainNavigationView = (NavigationView) findViewById(R.id.main_navigation_view);
    mainContentLayout = (CoordinatorLayout) findViewById(R.id.main_content_layout);

    // get user information
    NetworkController.getInstance().getCurrentUser(Utils.getAuthenticationToken(), new NetworkController.GetUserResponseCallback() {
        @Override
        public void onGetUser(Response<UserResponse> response) {
            if(response.isSuccessful() && response.body() != null && response.body().isSuccess()) {
                mainViewModel.setCurrentUser(response.body().getUser());
            }
        }

        @Override
        public void onRequestFailed(Throwable throwable) {

        }
    });

    mainViewModel.getCurrentUser().observe(this, new Observer<User>() {
        @Override
        public void onChanged(@Nullable User user) {
            TextView textUsername = (TextView) mainNavigationView.findViewById(R.id.drawer_header_username);
            if(textUsername != null) {
                textUsername.setText(user.getProfile().getUsername());
            }
        }
    });
}
activity\u main\u content.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:id="@+id/main_drawer_layout">

    <include
        layout="@layout/activity_main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/main_navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/drawer_menu_header"
        app:menu="@menu/main_drawer_menu">

    </android.support.design.widget.NavigationView>

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

    <!-- setup the toolbar -->
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="@dimen/toolbar_elevation"
            android:popupTheme="@style/AppTheme.PopupOverlay">

        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <!-- include the fragment container -->
    <android.support.constraint.ConstraintLayout
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:activity="com.fishley.android.activities.MainActivity"
        android:id="@+id/main_fragment_container"
        android:paddingTop="?attr/actionBarSize">

    </android.support.constraint.ConstraintLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_new_post"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_plus"
        android:tint="@android:color/white"
        android:visibility="gone"/>

</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    android:background="@drawable/drawer_menu_header_background">

    <TextView
        android:id="@+id/drawer_header_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Android Studio"
        android:textColor="@android:color/black"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:text="android.studio@android.com"/>

</LinearLayout>

如果您使用
setContentView
布局是同步充气的,因此不能部分充气,那么
mainNavigationView
有多少子视图?发布你的
布局。活动\u main
我已经用布局文件更新了我的答案。如果
setContentView
是同步的,则肯定是另一个问题。代码有时确实有效。嗯,很奇怪,你能不能,仅仅为了测试,使用一个自定义的
NavigationView
和覆盖的
inflateHeaderView
方法-你只需调用
super
和一些
Log.d
来查看它何时被调用?我尝试了你的建议。关于膨胀标题视图的日志语句始终位于关于TextView为
null
的日志语句之前。但是,我只是尝试像这样访问TextView:
TextView textUsername=(TextView)mainNavigationView.getHeaderView(0).findViewById(R.id.drawer\u header\u username)这似乎有效。。。不过,很难说问题是周期性的。