Android Api 23 Cambiar Navegación Ver headerLayout textview
Estoy probando el proyecto de ejemplo de Gaveta de navegación en android y tengo un problema al configurar el texto en el encabezado del perfil de vista de navegación.
Este es mi código:
- Mostrar todos los caracteres Unicode en TextView
- Cambio de color de TextView en el enfoque / presione
- Android scroll down
- TextView`s LinkMovementMethod bloquea el evento de toque listitem
- Tamaño de fuente a grande para caber en caché para Emoji en Kitkat solamente
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>
Cuando intento fijar el texto del textview que consigo siempre este error y sucede solamente con api nivel 23:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
¿Cómo puedo cambiar el texto Textview de la nav_header_main.xml de la actividad principal ??
Gracias por adelantado
- ¿Cómo manejar texto largo en las preferencias de Android?
- ¿Hay una manera de desactivar el ajuste de línea en una vista de lista en android?
- Cómo hacer que TextView se desplace en Android
- Envolvente de texto de Android
- ¿Cómo puedo cambiar la parte del color de un TextView?
- Desplazamiento horizontal del texto en Android
- Fade out / fade en TextView basado en desplazamiento
- Android - Recursos $ NotFoundException
Tuve el mismo problema y pude evitarlo con este código:
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");
Ahora con la versión 23.1.1 de la biblioteca de soporte de diseño, puede utilizar
View header = navigationView.getHeaderView(0) TextView text = (TextView) header.findViewById(R.id.textView);
La respuesta de Orium funciona y esto funcionará también.
View header = navigationView.inflateHeaderView(R.layout.nav_header_main); TextView text = (TextView) header.findViewById(R.id.textView); texto.setText("HELLO");
Esto resolvió mi problema
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);
O si utiliza este atributo:
<android.support.desibn.widget.NavigationView> ... app:headerLayout="@layout/your_header_layout"/>
Usted puede:
View header = mNavigationView.getHeaderView(0); TextView title = (TextView) header.findViewById(R.id.your_title_id); //Or something else
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);
- La elección de la foto con la nueva aplicación Google Fotos está rota
- ¿Cómo encontrar los registros en android studio?