【问题标题】:can't access to TextView无法访问 TextView
【发布时间】:2016-04-06 21:56:40
【问题描述】:

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

可能是代码错误,系统库更新后停止正常工作。

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

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>

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

【问题讨论】:

  • 您使用的是什么 IDE?你试过清理项目吗?
  • AndroidStudio 1.4,怎么办?

标签: android android-support-library


【解决方案1】:
    navigationView = (NavigationView)findViewById(R.id.nav_header_main);
    View headerLayout = navigationView.getHeaderView(0);
    tvUserName = (TextView) headerLayout.findViewById(R.id.navUserName);

【讨论】:

  • @user5546244 试试这个
  • @user5546244 因为我们需要指定标题位置。
  • 今天之前没有。当我编写它时(可能是 2 个月前)它工作正常,现在我看到它不是正确的代码,但是今天在 IDE 中发生了什么
【解决方案2】:
TextView tv = (TextView)navigationView.findViewById(id);

因为TextView在headerLayout中,而headerLayout是navigationView的根布局。

这个答案是错误的。

get more here

【讨论】:

  • 我添加了navigationView - 没有任何结果同样的问题
  • 抱歉我的回答没有经过验证。 IgorB 的回答比我的更接近。
【解决方案3】:

主要原因是支持库的版本变化

来自

compile 'com.android.support:design:23.0.1'

compile 'com.android.support:design:23.1.1'

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

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

而且这段代码不能通用“无法识别方法getHeaderView”

View headerLayout = navigationView.getHeaderView(0);

更改支持库版本时要小心,开发人员可以更改所有内容而不会发出任何警告。没有背部支撑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2014-01-29
    相关资源
    最近更新 更多