37

मैं एंड्रॉइड में नेविगेशन ड्रॉवर नमूना प्रोजेक्ट का परीक्षण कर रहा हूं और मुझे नेविगेशन व्यू प्रोफाइल हेडर में टेक्स्ट सेट करने में समस्या है।एंड्रॉइड एपीआई 23 नेविगेशन चेंज हेडर लेआउट टेक्स्टव्यू

यह मेरा कोड है:

MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 

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

    TextView text = (TextView) findViewById(R.id.textView); 
    texto.setText("HELLO"); 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<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> 

nav_header_main.xml

<?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="@dimen/nav_header_height" 
android:background="@drawable/side_nav_bar" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical" 
android:gravity="bottom"> 

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

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

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:text="[email protected]" android:id="@+id/textView" /> 

</LinearLayout> 

मैं पाठ सेट करने के लिए प्रयास करते हैं textv के iew मैं हमेशा इस त्रुटि मिलती है और यह केवल एपीआई स्तर 23 के साथ होता है:

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

मैं मुख्य गतिविधि से nav_header_main.xml की TextView पाठ कैसे बदल सकता हूँ ??

अग्रिम

+0

एक ही समस्या हो रही। शायद पुस्तकालय के साथ एक समस्या है? – Bignadad

+3

मुझे यह समस्या भी है - com.android.support:* 23.1.0 में निर्भरताओं के साथ पेश किया गया। इसे 23.0.1 पर वापस करने से इसे ठीक किया गया है, इसलिए जब तक इसे संबोधित नहीं किया जाता है तब तक मैं उसके साथ रहूंगा। –

+4

यहां एक ही समस्या है ... मुझे लगता है कि यह नया 'com.android.support:* 23.1.0' में एक बग है .... –

उत्तर

45

धन्यवाद मैं एक ही मुद्दा था और इस कोड के साथ यह से बचने के लिए कर रहा था:

View header = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null); 
    navigationView.addHeaderView(header); 
    TextView text = (TextView) header.findViewById(R.id.textView); 
    text.setText("HELLO"); 
+0

ग्रेट उत्तर। सही काम करता है – Bignadad

+3

इसे ऑन-ड्रावर ओपनेड श्रोता में सेट करना भी – user3065901

+0

काम करता है यदि यह एकमात्र समाधान है, तो हमारे पास एक्सएमएल क्यों है? xml के माध्यम से बदलने के लिए चारों ओर एक काम नहीं है? – bks4line

54
डिजाइन समर्थन पुस्तकालय की 23.1.1 रिलीज के साथ

अब, आप

उपयोग कर सकते हैं
View header = navigationView.getHeaderView(0) 
TextView text = (TextView) header.findViewById(R.id.textView); 
+0

Thx बहुत @German! वह काम किया। –

+1

यदि आप किसी भी कारण से 1 से अधिक शीर्षलेख रखते हैं तो आप 'navigationView.getHeaderCount()' का उपयोग भी कर सकते हैं। –

+0

मेरे लिए काम किया, धन्यवाद! –

7

ओरिम उत्तर काम करता है और यह भी काम करेगा।

View header = navigationView.inflateHeaderView(R.layout.nav_header_main); 
    TextView text = (TextView) header.findViewById(R.id.textView); 
    texto.setText("HELLO"); 
-1
View headerView=navigationView.getChildAt(0); 
TextView tvName=(TextView)hView.findViewById(R.id.name); 
TextView tvEmail=(TextView)hView.findViewById(R.id.email); 

    tvName.setText(data); 
    tvEmail.setText(data); 
+0

यह कोड मेरे लिए ठीक काम कर रहा है –

1
इस

के लिए मुझे

View headerView = navigationView.inflateHeaderView(R.layout.header_view); 

    txt_fullname = (TextView) headerView.findViewById(R.id.txt_fullname); 
    txt_email = (TextView) headerView.findViewById(R.id.txt_email); 
0

समस्या हल हो या फिर आप इस विशेषता का उपयोग करता है, तो:

<android.support.desibn.widget.NavigationView> 
    ... 
    app:headerLayout="@layout/your_header_layout"/> 

आप कर सकते हैं:

View header = mNavigationView.getHeaderView(0); 
TextView title = (TextView) header.findViewById(R.id.your_title_id); 
//Or something else 
संबंधित मुद्दे