我一直收到 "尝试在一个空对象引用上调用虚拟方法'android.view.View com.google.android.material.navigation.NavigationView.getHeaderView(int)'",即使对象已经声明。
导致错误的那行代码如下图所示。
nv_req = (NavigationView) findViewById(R.id.requester_navView);
reqHeaderView = nv_req.getHeaderView(0);
这段代码是在MainActivity的OnCreate方法中,并且在OnCreate之前声明了nv_req,reqHeaderView作为类的实例变量。
private NavigationView nv_req;
private View reqHeaderView;
另外,我的类扩展了AppCompactActivity,它不是一个片段。
包含导航视图的xml文件如下图所示。
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/requester_layout"
android:fitsSystemWindows="true"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".RequesterHome">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:id="@+id/requester_toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp">
</androidx.appcompat.widget.Toolbar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/requester_fragment">
</FrameLayout>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="@+id/requester_navView"
app:headerLayout="@layout/navigation_header"
app:menu="@menu/requester_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
这是我的onCreate方法 我看到有人在片段中使用onCreateView来解决这个问题,但是我不知道如何在扩展AppCompatActivity的类中使用onCreateView。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_email = (EditText) findViewById(R.id.et_logIn_email);
et_password = (EditText) findViewById(R.id.et_logIn_password);
bt_signUp = (Button) findViewById(R.id.bt_logIn_signUp);
pb_submit = (ProgressBar) findViewById(R.id.pb_signUp_submit);
firebaseAuth = FirebaseAuth.getInstance();
nv_req = (NavigationView) findViewById(R.id.requester_navView);
reqHeaderView = nv_req.getHeaderView(0);
tv_navReqUsername = (TextView) reqHeaderView.findViewById(R.id.tv_navHead_username);
tv_navReqEmail = (TextView) reqHeaderView.findViewById(R.id.tv_navHead_email);
nv_cab = (NavigationView) findViewById(R.id.cabinet_navView);
cabHeaderView = nv_cab.getHeaderView(0);
tv_navCabUsername = (TextView) cabHeaderView.findViewById(R.id.tv_navHead_username);
tv_navCabEmail = (TextView) reqHeaderView.findViewById(R.id.tv_navHead_email);
nv_mem = (NavigationView) findViewById(R.id.member_navView);
memHeaderView = nv_mem.getHeaderView(0);
tv_navMemUsername = (TextView) memHeaderView.findViewById(R.id.tv_navHead_username);
tv_navMemEmail = (TextView) reqHeaderView.findViewById(R.id.tv_navHead_email);
bt_signUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent signUpIntent = new Intent(MainActivity.this, SignUpActivity.class);
startActivity(signUpIntent);
finish();
}
});
}
我自己想明白了,我引用的导航视图不在activity_main.xml中,而activity的内容视图中。因此找不到这个组件。但是,我并没有意识到这一点,因为用正确的语法还是可以引用该组件的。当应用程序开始运行时,导航视图找不到了。