我正在使用NavigationView,它具有标题布局,该标题布局包含我要在我的Activity中尝试访问的textview,并且它返回null。

试图花很多时间在它上面,没有希望。

我的 Activity

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>

最佳答案

初始化 NavigationView 后,您将捕获带有NavigationView.getHeaderView(0)膨胀的 header ,并使用findViewById获取 View

navigationView = (NavigationView) findViewById(R.id.nav_view);
(TextView) navigationView.getHeaderView(0).findViewById(R.id.viewName);

关于android - 来自NavigationView header 的TextView返回null,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33540090/

10-12 01:38