Android 无法访问文本视图

Android 无法访问文本视图,android,android-support-library,Android,Android Support Library,我有活动。我尝试将text设置为TextView tvUserName。 直到今天,一切都正常,但现在我无法通过findViewById访问此变量。此变量的值为空 可能是错误的代码,并且在系统libs更新后停止正常工作 我没有做什么特别的事,也不知道该做什么 Caused by: java.lang.NullPointerException 当我尝试将文本设置为此变量时 private static TextView tvUserName; @Override protect

我有活动。我尝试将text设置为TextView tvUserName。 直到今天,一切都正常,但现在我无法通过findViewById访问此变量。此变量的值为空

可能是错误的代码,并且在系统libs更新后停止正常工作

我没有做什么特别的事,也不知道该做什么

Caused by: java.lang.NullPointerException
当我尝试将文本设置为此变量时

private static TextView tvUserName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        currentLocale = getResources().getConfiguration().locale;
        LocaleHelper.onCreate(this, currentLocale + "");
        setContentView(R.layout.activity_main);

        context = this;

        fragmentManager = getSupportFragmentManager();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(MainActivity.this);
        navigationView.setEnabled(false);

        DrawerLayout mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        tvUserName = (TextView) findViewById(R.id.navUserName);

        //?????
    }
-----activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <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_width="match_parent" android:layout_height="match_parent"
        android:fitsSystemWindows="true" tools:openDrawer="start">

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

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

        </android.support.v4.widget.DrawerLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:background="@drawable/side_nav_bar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
    android:gravity="bottom">

    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Phone4Pay"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="2dp"
        android:paddingBottom="15dp" >
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/navUserName"/>

    </LinearLayout>
</LinearLayout>
----导航总管总管---------

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <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_width="match_parent" android:layout_height="match_parent"
        android:fitsSystemWindows="true" tools:openDrawer="start">

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

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

        </android.support.v4.widget.DrawerLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:paddingLeft="16dp"
    android:background="@drawable/side_nav_bar"
    android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
    android:gravity="bottom">

    <TextView android:layout_width="match_parent" android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Phone4Pay"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="2dp"
        android:paddingBottom="15dp" >
        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/navUserName"/>

    </LinearLayout>
</LinearLayout>
原因TextView位于headerLayout中,headerLayout是navigationView的根布局

这个答案是错误的

原因TextView位于headerLayout中,headerLayout是navigationView的根布局

这个答案是错误的


主要原因是更改了支持库的版本

但在旧版本23.0.1中,此代码可以正常工作:

tvUserName = (TextView) findViewById(R.id.navUserName);
这段代码不通用,无法识别getHeaderView方法

View headerLayout = navigationView.getHeaderView(0);

更改支持库版本时要小心,开发人员可以在没有任何警告的情况下更改所有内容。没有后台支持。

主要原因是更改了支持库的版本

但在旧版本23.0.1中,此代码可以正常工作:

tvUserName = (TextView) findViewById(R.id.navUserName);
这段代码不通用,无法识别getHeaderView方法

View headerLayout = navigationView.getHeaderView(0);

更改支持库版本时要小心,开发人员可以在没有任何警告的情况下更改所有内容。无背面支持。

您使用的是什么IDE?你试过清理这个项目吗?AndroidStudio 1.4,怎么做?你用的是什么IDE?你试过清理这个项目吗?AndroidStudio 1.4,怎么做?我添加了navigationView-没有任何结果,同样的问题对不起,我的答案没有经过验证。IgorB的答案比我的更接近。我添加了navigationView-没有任何结果,同样的问题对不起,我的答案没有经过验证。IgorB的答案比我的更接近。@user5546244请尝试this@user5546244因为我们需要采取特定的头球位置。今天之前不行。当我在2个月前编写它时,它还可以正常工作,现在我看到它不是正确的代码,但是IDE中发生了什么today@user5546244尝试this@user5546244因为我们需要采取特定的头球位置。今天之前不行。当我在2个月前编写它时,它还可以正常工作,现在我看到它不是正确的代码,但今天在IDE中发生了什么