将json信息放入包含在主布局中的NavigationView布局中?

时间:2018-07-05 10:00:42

标签: android json node.js

我在抽屉里工作,我想在标题中获取一些信息,例如google示例中的图标,名称和标题

我得到的错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.xbxvxe.rndfy, PID: 4467
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xbxvxe.rndfy/com.xbxvxe.rndfy.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.NavigationView.findViewById(int)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                      at android.app.ActivityThread.-wrap11(Unknown Source:0)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                      at android.os.Handler.dispatchMessage(Handler.java:106)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6494)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.design.widget.NavigationView.findViewById(int)' on a null object reference
                      at com.xbxvxe.rndfy.activity.MainActivity.onCreate(MainActivity.java:88)
                      at android.app.Activity.performCreate(Activity.java:7009)
                      at android.app.Activity.performCreate(Activity.java:7000)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                      at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                      at android.os.Handler.dispatchMessage(Handler.java:106) 
                      at android.os.Looper.loop(Looper.java:164) 
                      at android.app.ActivityThread.main(ActivityThread.java:6494) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

我进入这一行:

 header = (PercentRelativeLayout) navigationView.findViewById(R.id.navigation_drawer_account_section);

我的主要课程是:

// @BindView(R.id.navigation_drawer_header)
NavigationView navigationView;
PercentRelativeLayout header;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init(savedInstanceState);


    //display data of user
    txtFullName = (TextView) findViewById(R.id.fullname);
    // SqLite database handler
    db = new SQLiteHandler(getApplicationContext());

    // session manager
    session = new SessionManager(getApplicationContext());

    if (!session.isLoggedIn()) {
        logoutUser();
    }


    // Fetching user details from SQLite
    HashMap<String, String> user = db.getUserDetails();

    String nom = user.get("nom");
    String prenom = user.get("prenom");

    // Navigation header


     navigationView = (NavigationView) this.findViewById(R.id.navigation_drawer_header);
     header = (PercentRelativeLayout) navigationView.findViewById(R.id.navigation_drawer_account_section);

    // Displaying the user details on the screen

    TextView txtFullName = (TextView) header.findViewById(R.id.fullname);
    txtFullName.setText(nom + " " + prenom);

}

我的主要布局:

<?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/main_activity_DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<!-- The main content view -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/main_activity_content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/activity_main_navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:maxWidth="@dimen/navigation_drawer_max_width"
    app:elevation="@dimen/elevation_nav_drawer"
    app:headerLayout="@layout/navigation_drawer_header"
    app:menu="@menu/navigation_drawer_body" />

</android.support.v4.widget.DrawerLayout>

我的标题布局是这样的:

<?xml version="1.0" encoding="utf-8"?>

<android.support.percent.PercentRelativeLayout 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/navigation_drawer_header"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  tools:context="com.xbxvxe.rndfy.activity.MainActivity">

  <FrameLayout
    android:id="@+id/navigation_drawer_header_clickable"
    android:clickable="true"
    android:foreground="@drawable/nav_drawer_header_foreground"
    app:layout_aspectRatio="178%"
    app:layout_widthPercent="100%">

    <ImageView
      android:id="@+id/navigation_drawer_user_account_picture_cover"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scaleType="centerCrop"
      android:src="@drawable/image_nav_drawer_account_background"
      tools:ignore="ContentDescription" />

  </FrameLayout>

  <LinearLayout
    android:id="@+id/navigation_drawer_account_section"
    android:paddingBottom="@dimen/spacing_8"
    android:paddingStart="@dimen/vertical_keyline_first"
    android:paddingEnd="@dimen/vertical_keyline_first"
    android:paddingLeft="@dimen/vertical_keyline_first"
    android:paddingRight="@dimen/vertical_keyline_first"
    android:gravity="bottom"
    android:orientation="vertical"
    app:layout_aspectRatio="178%"
    app:layout_widthPercent="100%">

    <ImageView
      android:id="@+id/navigation_drawer_user_account_picture_profile"
      android:layout_width="@dimen/nav_drawer_profile_image_size"
      android:layout_height="@dimen/nav_drawer_profile_image_size"
      app:srcCompat="@drawable/ic_account_circle_white_64dp"
      tools:ignore="ContentDescription" />

    <LinearLayout
      android:layout_width="wrap_content"
      android:layout_height="@dimen/spacing_56"
      android:gravity="center_vertical"
      android:orientation="vertical">

      <TextView
          android:id="@+id/fullname"
          android:layout_width="215dp"
          android:layout_height="32dp"
          android:text="@string/fullname"
          android:textSize="13dp" />

      <TextView
          android:id="@+id/email"
          android:layout_width="215dp"
          android:layout_height="32dp"
          android:text="@string/email"
          android:textSize="13dp" />

    </LinearLayout>

  </LinearLayout>

</android.support.percent.PercentRelativeLayout>

3 个答案:

答案 0 :(得分:0)

您定义您的

  • NavigationView,其ID为activity_main_navigation_view,而不是navigation_drawer_header

因此您需要正确投射ID。

更改

navigationView = (NavigationView) this.findViewById(R.id.activity_main_navigation_view);

代替:

navigationView = (NavigationView) this.findViewById(R.id.navigation_drawer_header);

然后,获取导航标题:

View header = navigationView.inflateHeaderView(R.layout.your_navigation_header);

答案 1 :(得分:0)

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    navigationView.setItemIconTintList(null);
    View headerLayout = navigationView.getHeaderView(0);
    final TextView user_name = (TextView) headerLayout.findViewById(R.id.user_name);

答案 2 :(得分:0)

更正此问题之后:-

navigationView = (NavigationView) this.findViewById(R.id.activity_main_navigation_view);

删除行:-

header = (PercentRelativeLayout) navigationView.findViewById(R.id.navigation_drawer_account_section);

并添加以下代码:-

 View header = navigationView.getHeaderView(0);

// Displaying the user details on the screen

TextView txtFullName = (TextView) header.findViewById(R.id.fullname);
txtFullName.setText(nom + " " + prenom);