2017-03-02 99 views
-1

嘗試在menuText上設置setText()時,我總是收到一個空指針。Android TextView保持爲空

我的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" /> 

頭:

menuText.setText("text goes here"); 

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

這是工作沒有問題,異常被拋出

例外:

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保持爲空,甚至嘗試用刀切割注射。

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

仍然不起作用。有什麼建議麼?

+0

你在哪裏調用'setText'? – Vyacheslav

+0

findViewById(R.id.personals)可能返回null。 –

+0

請確保textview與id「personals」包含在activity_main –

回答

1

要successfuly尋找小部件抽屜式導航欄的標題,你需要通過它調用getHeaderView()首先得到正確的容器視圖:

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

,然後有header指向右側的ViewGroup可以像往常一樣爲其目標窗口小部件調用findViewById()

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

另外,如果您正在搜索自己,您使用的是@InjectView

+0

謝謝,這有幫助,並且是有用的,我仍然需要了解爲什麼在我做出的更改之前工作。 – user1794974

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"); 
1
NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);   
View header = navigationView.getHeaderView(0); 
TextView username = (TextView) header.findViewById(R.id.personals); 
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"); 

嘗試它,它會肯定

+0

接受這個答案,如果它幫助你。 –

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

這應該工作。

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");