0
votes

[this is the screenshot ][1]Can you help me please, the header(email and an image of profil) is not found, this the code 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: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"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

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

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="aaaa.com" />

</LinearLayout>`

main_pro.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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:context=".tracking.MainPro">
<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.DrrawerLayout>

the error :Error:(2) No resource identifier found for attribute 'context' in package 'com....'

08-12 16:57:52.849 5438-5438/.gps.tracking E/AndroidRuntime: FATAL EXCEPTION: main Process: gps.tracking, PID: 5438 java.lang.RuntimeException: Unable to start activity ComponentInfo{.gps.tracking/.gps.tracking.MainPro}: android.view.InflateException: Binary XML file line #15: Error inflating class android.support.design.widget.NavigationView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471) at android.app.ActivityThread.access$900(ActivityThread.java:175) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class android.support.design.widget.NavigationView at android.view.LayoutInflater.createView(LayoutInflater.java:626) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) at android.view.LayoutInflater.inflate(LayoutInflater.java:498) at android.view.LayoutInflater.inflate(LayoutInflater.java:398) at android.view.LayoutInflater.inflate(LayoutInflater.java:354) at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249) at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106) at gps.tracking.MainPro.onCreate(MainPro.java:31) at android.app.Activity.performCreate(Activity.java:5451) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)  at android.app.ActivityThread.access$900(ActivityThread.java:175)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:146)  at android.app.ActivityThread.main(ActivityThread.java:5602)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)  at dalvik.system.NativeStart.main(Native Method)  Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at android.view.LayoutInflater.createView(LayoutInflater.java:600) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)  at android.view.LayoutInflater.inflate(LayoutInflater.java:498)  at android.view.LayoutInflater.inflate(LayoutInflater.java:398)  at android.view.LayoutInflater.inflate(LayoutInflater.java:354)  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)  at gps.tracking.MainPro.onCreate(MainPro.java:31)  at android.app.Activity.performCreate(Activity.java:5451)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)  at android.app.ActivityThread.access$900(ActivityThread.java:175)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:146)  at android.app.ActivityThread.main(ActivityThread.java:5602)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)  at dalvik.system.NativeStart.main(Native Method)  Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class android.widget.ImageView at android.view.LayoutInflater.createView(LayoutInflater.java:626) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700) at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) at android.view.LayoutInflater.inflate(LayoutInflater.java:498) at android.view.LayoutInflater.inflate(LayoutInflater.java:398) at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:184) at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:227) at android.support.design.widget.NavigationView.(NavigationView.java:152) at android.support.design.widget.NavigationView.(NavigationView.java:88) at java.lang.reflect.Constructor.constructNative(Native Method)  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)  at android.view.LayoutInflater.createView(LayoutInflater.java:600)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)  at android.view.LayoutInflater.inflate(LayoutInflater.java:498)  at android.view.LayoutInflater.inflate(LayoutInflater.java:398)  at android.view.LayoutInflater.inflate(LayoutInflater.java:354)  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)  at .gps.tracking.MainPro.onCreate(MainPro.java:31)  at android.app.Activity.performCreate(Activity.java:5451)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)  at android.app.ActivityThread.access$900(ActivityThread.java:175)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:146)  at android.app.ActivityThread.main(ActivityThread.java:5602)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)  at dalvik.system.NativeStart.main(Native Method)  Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at android.view.LayoutInflater.createView(LayoutInflater.java:600) at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)  at android.view.LayoutInflater.inflate(LayoutInflater.java:498)  at android.view.LayoutInflater.inflate(LayoutInflater.java:398)  at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:184)  at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:227)  at android.support.design.widget.NavigationView.(NavigationView.java:152)  at android.support.design.widget.NavigationView.(NavigationView.java:88)  at java.lang.reflect.Constructor.constructNative(Native Method)  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)  at android.view.LayoutInflater.createView(LayoutInflater.java:600)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)  at android.view.LayoutInflater.inflate(LayoutInflater.java:498)  at android.view.LayoutInflater.inflate(LayoutInflater.java:398)  at android.view.LayoutInflater.inflate(LayoutInflater.java:354)  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)  at com.sifast.gps.tracking.MainPro.onCreate(MainPro.java:31)  at android.app.Activity.performCreate(Activity.java:5451)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)  at android.app.ActivityThread.access$900(ActivityThread.java:175)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:146)  at android.app.ActivityThread.main(ActivityThread.java:5602)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)  at dalvik.system.NativeStart.main(Native Method)  Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f020043 a=-1 r=0x7f020043} at android.content.res.Resources.loadDrawable(Resources.java:3412) at android.content.res.TypedArray.getDrawable(TypedArray.java:602) at android.widget.ImageView.(ImageView.java:133) at android.widget.ImageView.(ImageView.java:123) at java.lang.reflect.Constructor.constructNative(Native Method)  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)  at android.view.LayoutInflater.createView(LayoutInflater.java:600)  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:675)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:700)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)  at android.view.LayoutInflater.inflate(LayoutInflater.java:498)  at android.view.LayoutInflater.inflate(LayoutInflater.java:398)  at android.support.design.internal.NavigationMenuPresenter.inflateHeaderView(NavigationMenuPresenter.java:184)  at android.support.design.widget.NavigationView.inflateHeaderView(NavigationView.java:227)  at android.support.design.widget.NavigationView.(NavigationView.java:152)  at android.support.design.widget.NavigationView.(NavigationView.java:88)  at java.lang.reflect.Constructor.constructNative(Native Method)  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)  at android.view.LayoutInflater.createView(LayoutInflater.java:600)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)  at android.view.LayoutInflater.inflate(LayoutInflater.java:498)  at android.view.LayoutInflater.inflate(LayoutInflater.java:398)  at android.view.LayoutInflater.inflate(LayoutInflater.java:354)  at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)  at gps.tracking.MainPro.onCreate(MainPro.java:31)  at android.app.Activity.performCreate(Activity.java:5451)  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)  at android.app.ActivityThread.access$900(ActivityThread.java:175)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:146)  at android.app.ActivityThread.main(ActivityThread.java:5602)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)  at dalvik.system.NativeStart.main(Native Method)  08-12 17:00:00.674 5438-5438/gps.tracking I/Process: Sending signal. PID: 5438 SIG: 9

1
Please give more details - Siddharth Venu
What is the problem can you please elaborate more. - Jay Rathod RJ
the menu exist but the header of the layout dosen't appear, i can't inderstand the problem - Ammy19
Do you get the problem in java file ? - rushank shah
Show the code where you make use of this XML file, and describe what you mean by "not found". You cannot retrieve the views with findViewById()? - Bryan

1 Answers

2
votes

The NavigationView internally uses a RecyclerView to display the headers and menu items. This causes the activity to not find any views in the header, as the RecyclerView dynamically loads the views (that's the whole purpose of it).

However, the support library has a method for getting the header view, as documented in the NavigationView API reference:

getHeaderView(int index): Gets the header view at the specified position.

So the following piece of code should get the profile ImageView and the email TextView:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
View headerView = navigationView.getHeaderView(0);
ImageView profileView = (ImageView) headerView.findViewById(R.id.profil);
TextView emailView = (TextView) headerView.findViewById(R.id.textView);

Edit: Based on the stack trace you provided the core issue is the following (@drawable/profil is apparantly not a valid drawable or doesn't exist):

android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path)