2016-11-01 3 views
-1

android studio 2.2에서 작업 중이며 탐색 서랍 작업을 사용했습니다. 코드에서 nav_header_layout의 텍스트를 변경하고 싶습니다만, 코드에서 findViewById를 사용하여 nav_header_layout에있는 TextView를 인덱싱하면 null이 반환됩니다. 그것을 고칠 방법이 있습니까?텍스트보기 return null

여기

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

자바 코드

@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에 액세스하려면 액세스 할 수있는 XML을합니다.

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