NavigationDrawer中的NullPointException在android中的头文件

时间:2018-03-19 08:58:49

标签: android xml android-layout nullpointerexception navigation-drawer

您好我试图找到每个解决方案,但没有解决方案正在解决我的问题。我在android中制作导航画,但是这部分的头部显示零点异常我怎么能解决这个问题

活动代码是

navigationView = findViewById(R.id.nav_view);
    headerView = getLayoutInflater().from(this).inflate(R.layout.nav_header_main, null);
    navigationView.addHeaderView(headerView);

    profile = headerView.findViewById(R.id.img_profile);
    name = headerView.findViewById(R.id.name);
    companyname = headerView.findViewById(R.id.companyname);

这是Actitvty的xml,我宣布了android的导航程序

 <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="match_parent"
    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.design.widget.NavigationView>

这是标题xml

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:orientation="horizontal"
    android:padding="@dimen/_16sdp">

    <ImageView
        android:id="@+id/img_profile"
        android:layout_width="@dimen/_90sdp"
        android:layout_height="@dimen/_90sdp"
        android:layout_marginRight="@dimen/_10sdp"
        android:padding="@dimen/_2sdp"
        android:paddingTop="@dimen/_16sdp"
        android:src="@drawable/companylogo" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_12sdp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/_1sdp"
            android:text="Welcome!"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@color/second_color"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/companyname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/_10sdp"
            android:text="Mr Salesman"
            android:textAppearance="@style/TextAppearance.AppCompat.Body1"
            android:textColor="@color/second_color"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/website"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GLINT LOGIC"
            android:textColor="@color/second_color"
            android:textStyle="bold" />
    </LinearLayout>

这是导致XML的错误,我的应用程序被Android

杀死
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.NavigationView.inflateHeaderView(int)' on a null object reference
at com.example.aka.gism.all_activity.MainActivity.onCreate(MainActivity.java:85)
at android.app.Activity.performCreate(Activity.java:6964)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1573) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:156) 
at android.app.ActivityThread.main(ActivityThread.java:6605) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:999) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:889) 

2 个答案:

答案 0 :(得分:0)

您不需要在导航视图中夸大布局标题,而不是此

headerView = getLayoutInflater().from(this).inflate(R.layout.nav_header_main, null);
navigationView.addHeaderView(headerView);

使用

headerView = navigationView.getHeaderView(0) // 0 is first header
profile = headerView.findViewById(R.id.imageProfile);

答案 1 :(得分:0)

试试这个,

navigationView = findViewById(R.id.nav_view);
View header=navigationView.getHeaderView(0);

profile = header.findViewById(R.id.img_profile);
name = header.findViewById(R.id.name);
companyname = header.findViewById(R.id.companyname);