我正在使用具有标题布局的NavigationView,该标题布局包含我试图在我的Activity中访问的textview,它返回null。
尝试花费大量时间,没有希望。
我的活动
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
RelativeLayout headerLL;
ImageView userIV;
TextView usernameTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
headerLL = (RelativeLayout) findViewById(R.id.navHeaderLL);
userIV = (ImageView) findViewById(R.id.userDPIV);
usernameTV = (TextView) findViewById(R.id.usernameTV);
//getting error here
usernameTV.setText("Ashiq");
initializeDrawer();
}
}
activity_mail.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:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<include layout="@layout/include_list_viewpager"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_view"
/>
</android.support.v4.widget.DrawerLayout>
nav_header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="192dp"
xmlns:tools="http://schemas.android.com/tools"
android:background="?attr/colorPrimaryDark"
android:padding="16dp"
android:id="@+id/navHeaderLL"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/userDPIV"
android:elevation="4dp"
android:layout_above="@+id/usernameTV"
android:layout_marginBottom="10dp"
tools:src="@drawable/ic_menu"
/>
<TextView
android:id="@+id/usernameTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/TextViewAppearance.UserName"
tools:text="Username"
android:layout_alignParentBottom="true"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"/>
</RelativeLayout>
答案 0 :(得分:25)
初始化 NavigationView 后,您会抓住使用navigationView.getHeaderView(0)充气的标题,并使用findViewById获取视图
navigationView = (NavigationView) findViewById(R.id.nav_view);
(TextView) navigationView.getHeaderView(0).findViewById(R.id.viewName);
答案 1 :(得分:1)
公共类 MainActivity 扩展 AppCompatActivity {
private DrawerLayout mDrawerLayout;
RelativeLayout headerLL;
ImageView userIV;
TextView usernameTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
headerLL = (RelativeLayout) findViewById(R.id.navHeaderLL); // line to change
userIV = (ImageView) findViewById(R.id.userDPIV);
usernameTV = (TextView) findViewById(R.id.usernameTV);
//getting error here
usernameTV.setText("Ashiq");
initializeDrawer();
}
}
//对于“要更改的行”,您必须通过执行获取RootView
headerLL = (RelativeLayout) findViewById(R.id.navHeaderLL).getRootView();
答案 2 :(得分:0)
这样做。
2.之后,初始化导航视图元素
更改以下代码
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
RelativeLayout headerLL;
ImageView userIV;
TextView usernameTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1
initializeDrawer();
//2
headerLL = (RelativeLayout) findViewById(R.id.navHeaderLL);
userIV = (ImageView) findViewById(R.id.userDPIV);
usernameTV = (TextView) findViewById(R.id.usernameTV);
//getting error here
usernameTV.setText("Ashiq");
}
}
希望这有帮助。
快乐编码:)