我有一个MainActivity
包含NavigationView
抽屉菜单的。的NavigationView
标头包含一个TextView
我想用一些用户信息填充的。问题是,有时应用程序会在onChanged
方法中崩溃,NullPointerException
因为textUsername
is 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_main.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>
activity_main_content.xml
:
<?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>
drawer_menu_header.xml
:
<?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>