Reputation:
I have a TextView inside a layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_header"
android:layout_width="match_parent"
android:layout_height="@dimen/header_height"
android:background="@drawable/avena_nav_drawer"
android:gravity="bottom"
android:orientation="vertical"
android:padding="@dimen/header_left_padding"
style="@style/AppTheme">
<!-- Correo de la cuenta -->
<TextView
android:id="@+id/drawer_email"
style="@style/Widget.AppCompat.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/user_email"
android:textSize="14sp"
android:textColor="@color/chalk"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
</LinearLayout>
Now, such layout is acting as a header inside my drawerlayout, like this:
<android.support.design.widget.NavigationView
android:id="@+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="@color/chalk"
android:fitsSystemWindows="true"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/action_menu" />
I have been trying to reference my TextView from the Activity that inflates the layout which contains the NavigationView (also tried another tricky/fail-y ways), but it always throws a Null Object Reference.
Could anyone please help me?
EDIT
Here is the logcat:
01-07 22:59:00.149 4284-4284/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.avena.avena, PID: 4284
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.avena.avena/com.avena.avena.dashboard.Dashboard}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.avena.avena.dashboard.Dashboard.onCreate(Dashboard.java:70)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Here is the activity code:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
// Set and reference textview inside drawer
TextView drawer_email = (TextView) findViewById(R.id.drawer_email);
if (logged) {
drawer_email.setText(email);
} else {
drawer_email.setText("Invitado");
}
// Set layout
setContentView(R.layout.activity_main);
EDIT 2
Found the right answer here:
Control view from inside navigation view
Upvotes: 0
Views: 1496
Reputation: 1121
First you have to call
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
and then:
TextView drawer_email = (TextView) findViewById(R.id.drawer_email);
//and your rest of code..
Upvotes: 0
Reputation: 6345
Looking at your current code it seems that you are trying to access the TextView before you inflate the layout.
Try the following:
// Set layout
setContentView(R.layout.activity_main);
once you call setContentView
, this API Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.
call this before you access the TextView
.
TextView drawer_email = (TextView) view.findViewById(R.id.drawer_email);
Upvotes: 0
Reputation: 1043
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = LayoutInflater.from(context).inflate(R.layout... //whatever your inflating code is
...
// Set and reference textview inside drawer
//here use view to access your item
TextView drawer_email = (TextView) view.findViewById(R.id.drawer_email);
if (logged) {
drawer_email.setText(email);
} else {
drawer_email.setText("Invitado");
}
You can not access any item of the layout without setting the layout or without referencing the view.
So you need to set the view first then access the textview
Upvotes: 0
Reputation: 51
You are not inflating your layout, you should add:
setContentView(R.layout.yourLayoutNameHere);
After:
super.onCreate(savedInstanceState);
Upvotes: 1