2016-11-01 97 views
-1

我正在使用android studio 2.2并使用导航抽屉活动。那么我想从代码中更改nav_header_layout中的文本,但是当我在代码中使用findViewById来索引nav_header_layout中的TextView时,它将返回null。有什么方法可以解决它吗?文本视图返回null

这里是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" 
android:layout_width="match_parent" 
android:layout_height="@dimen/nav_header_height" 
xmlns:tools="http://schemas.android.com/tools" 
android:background="@drawable/side_nav_bar" 
android:gravity="bottom" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.nexmo.sdk.sample.verifysample.Dashboard" 
android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/nav_header_vertical_spacing" 
    app:srcCompat="@android:drawable/sym_def_app_icon" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/nav_header_vertical_spacing" 
    android:text="Android" 
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/nav_number"/> 

</LinearLayout> 

Java代码

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_dashboard); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    TextView textv=(TextView)findViewById (R.id.nav_number); 
    textv.setText("new string"); 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.container); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
    tabLayout.setupWithViewPager(mViewPager); 


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.setDrawerListener(toggle); 
    toggle.syncState(); 

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 

} 
+0

上面的xml是activity_dashboard.xml吗? – codeMagic

+0

我不能看到任何问题你的代码...检查是否引用正确的布局或不setContentView(R.layout.activity_dashboard –

+0

ViewPager,tablayout ??? –

回答

0

如果你想访问nav_header_layout,您可以访问。

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 

     View headerView = navigationView.getHeaderView(0); 
     if (headerView != null) { 
      TextView myTextView = (TextView) headerView.findViewById(R.id.ID_OF_YOUR_TEXTVIEW); 
      if(myTextView!=null){ 
       myTextView.setText("Hello"); 
      } 
     }