2016-03-04 63 views
1

我有MainActivite。我嘗試將文本設置爲TextView tvUserName。 直到今天一切正常,但現在我無法通過findViewById訪問此變量。此變量的值爲null。無法訪問TextView

也許這是一個錯誤的代碼,它會在系統庫更新後停止正常工作。

我沒有做任何特別的事情,不明白該怎麼做。

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

您使用的是什麼IDE?您嘗試清潔該項目? –

+0

AndroidStudio 1.4,該怎麼做? – koa73

回答

2
TextView tv = (TextView)navigationView.findViewById(id); 

原因TextView位於headerLayout中,而headerLayout是navigationView的根佈局。

這個答案是錯誤的。

get more here

+0

我添加了navigationView - 沒有任何結果同樣的問題 – koa73

+0

對不起,我的答案沒有驗證。通過IgorB答案比我的更接近。 – grantonzhuang

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

@ user5546244試試這個 – IgorB

+0

非常感謝!!!!!!!它幫助了我。爲什麼它的工作原理? – koa73

+0

@ user5546244,因爲我們需要指定標題位置。 – IgorB

0

的主要原因是從

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

改變支持版本的lib

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

但在舊版本(23.0.1)此代碼的工作確定:

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

這個代碼不常見的工作「不能識別方法getHeaderView」

View headerLayout = navigationView.getHeaderView(0); 

要小心,當你改變支持庫版本,開發者可以在沒有任何警告的情況下改變一切沒有後援。