Android TextView保持为null

时间:2017-03-02 11:58:11

标签: java android

我在menuText上尝试setText()时一直得到一个nullpointer。

my onCreate:

setContentView(R.layout.activity_main);
TextView menuText = (TextView) findViewById(R.id.personals);

activity_main:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar" />

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></FrameLayout>

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/header"
        app:itemIconTint="#4c3327"
        app:itemTextColor="#c75b12"
        app:menu="@menu/drawer" />

头:

<TextView
        android:id="@+id/personals"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="20dp"
        android:text="text"
        android:textColor="#FFF"
        android:textSize="15sp"
        android:textStyle="bold" />

这没有问题,例外情况是:

menuText.setText("text goes here");

例外:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

我不得不提的是工作正常,我改变了: com.android.support:appcompat-v7:25.1.0从22到25并添加 com.karumi:dexter:3.0.2,做了权限,现在遇到了这个问题。

无论我做什么,menuText都保持为null,甚至尝试用butterknife注入。

@InjectView(R.id.personals)
    TextView menuText;

仍然无效。有什么建议吗?

6 个答案:

答案 0 :(得分:1)

要成功在Navigation Drawer的标题中查找小部件,您需要先调用getHeaderView()来获取正确的容器视图:

View header = ((NavigationView)findViewById(R.id.navigation_view)).getHeaderView(0);

然后让header指向正确的ViewGroup可以像往常一样为其调用findViewById()目标小部件:

TextView iv = ((TextView) header.findViewById(R.id.personals));

另外,如果您正在搜索自己,使用@InjectView的内容是什么?

答案 1 :(得分:1)

 NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);        
 View header = navigationView.getHeaderView(0);
 TextView username = (TextView) header.findViewById(R.id.personals);

答案 2 :(得分:0)

请使用此代码

 NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View header=navigationView.getHeaderView(0);

menuText = (TextView)header.findViewById(R.id. personals);
menuText.setText("text goes here");

答案 3 :(得分:0)

NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
View headerView = vNavigation.getHeaderView(0);
TextView text = headerView.findViewById(R.id.personals);
text.setText("It is working now");

尝试它肯定会起作用

答案 4 :(得分:0)

NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);
View head = navigationView.getHeaderView(0);
TextView username = (TextView) head.findViewById(R.id.personals);

这应该有效。

答案 5 :(得分:0)

NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
View header=navigationView.getHeaderView(0);

TextView tv= (TextView)header.findViewById(R.id. personals);
tv.setText("text goes here");