无法访问TextView [英] can't access to TextView

查看:103
本文介绍了无法访问TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有MainActivite.我尝试将文本设置为TextView tvUserName. 直到今天一切都还好,但是现在我无法通过findViewById访问此变量.此变量的值为空.

I have MainActivite. And I try to set text to TextView tvUserName. Until today everything was Ok, but now I can't access to this variable by findViewById. Value of this variable is null.

也许是错误的代码,并且在系统库更新后停止正常工作.

Maybe it's a wrong code, and it's stopped to work correctly after system libs update.

我没有做任何特别的事情,也不知道该怎么办.

I didn't do anything special, and don't understand what to do.

Caused by: java.lang.NullPointerException

当我尝试将文本设置为此变量时

when I try to set text to this variable

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 ------------------

--------- 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>

--------- nav_header_main -----------------

------- nav_header_main -----------------

<?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>

推荐答案

    navigationView = (NavigationView)findViewById(R.id.nav_header_main);
    View headerLayout = navigationView.getHeaderView(0);
    tvUserName = (TextView) headerLayout.findViewById(R.id.navUserName);

这篇关于无法访问TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆