it-swarm.com.de

Android-API 23 Navigationsleiste ändern Header-Textansicht

Ich teste das Beispielprojekt "Navigation Drawer" in Android. Ich habe ein Problem mit dem Text in der Kopfzeile der Navigationsansicht.

Das ist mein Code:

Hauptaktivität.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>

Wenn ich versuche, den Text der Textansicht festzulegen, erhalte ich immer diese Fehlermeldung und dies geschieht nur mit API-Ebene 23:

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

Wie kann ich den Textview-Text der nav_header_main.xml von der Hauptaktivität aus ändern?

Danke im Voraus

37
user3065901

Ich hatte das gleiche Problem und konnte es mit diesem Code vermeiden:

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

Mit der Version 23.1.1 der Design-Support-Bibliothek können Sie nun verwenden

View header = navigationView.getHeaderView(0)
TextView text = (TextView) header.findViewById(R.id.textView);
54
German

Orium Antwort funktioniert und das wird auch funktionieren.

View header = navigationView.inflateHeaderView(R.layout.nav_header_main);
    TextView text = (TextView) header.findViewById(R.id.textView);
    texto.setText("HELLO");
7
Roni Soreen

das löste das Problem für mich 

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

Oder wenn Sie dieses Attribut verwenden:

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

Sie können:

View header = mNavigationView.getHeaderView(0);
TextView title = (TextView) header.findViewById(R.id.your_title_id);
//Or something else
0
TarikW
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
Mahadev Dalavi