Java 导航活动中出现Android错误
我在Android应用程序中有一个导航活动。我使用Android studio中的默认模板创建导航抽屉。 以下是Nav_header_main.xml中的代码Java 导航活动中出现Android错误,java,android,Java,Android,我在Android应用程序中有一个导航活动。我使用Android studio中的默认模板创建导航抽屉。 以下是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:l
<?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">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:id="@+id/Name"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/mailId" />
</LinearLayout>
}
下面是activity_home.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_home" 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="false"
app:headerLayout="@layout/nav_header_home" app:menu="@menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
)
我正在尝试在导航抽屉的标题部分设置名称。导航抽屉中的文本视图只有在拉出时才可见。我是Android编程新手,我不确定,如果您的视图位于导航视图中,是否有其他方法设置导航抽屉中的文本视图。请尝试从导航视图中查找视图
tvUserName=(TextView) navigationView.findViewById(R.id.Name);
tvMailId=(TextView) navigationView.findViewById(R.id.mailId);
您正在将
activity\u home.xml
膨胀为您的activity
主布局:
setContentView(R.layout.activity_home);
您可以这样分配文本视图:
tvUserName=(TextView)findViewById(R.id.Name);
tvMailId=(TextView)findViewById(R.id.mailId);
但是那些TextView
包含在名为Nav\u header\u main.xml
的版面中,您可以在版面中查找它们(activity\u home.xml
)
您需要在正确的布局中查找那些TextView
,否则它们将显示为null
,请尝试以下操作:
tvUserName=(TextView) navigationView.findViewById(R.id.Name);
tvMailId=(TextView) navigationView.findViewById(R.id.mailId);
编辑:以上假设您至少在活动
主布局中的某个位置使用导航头_main.xml
,例如使用包含
:
<include
id="@+id/nav_view"
layout="@layout/Nav_header_main"/>
user.name
为空。在您使用的登录日志中检查它:user.customerId
和user.mailId
,但您没有检查此客户的名称;-)我尝试了这个改变,但仍然得到相同的错误。问题中已更新。我尝试进行此更改,但仍然收到相同的错误。在问题中更新。可能是因为Nav_header_main.xml根本不是布局的一部分,请参阅我的编辑…它包含在活动_home中。在问题中添加了该代码。请查找编辑器包含文本视图的版面名为Nav_header_main,但您使用的另一个Nav_header_home(应用程序:headerLayout=“@layout/Nav_header_home”)到处都是Nav_header_home,我没有提到Nav_header_main
tvUserName=(TextView) navigationView.findViewById(R.id.Name);
tvMailId=(TextView) navigationView.findViewById(R.id.mailId);
<include
id="@+id/nav_view"
layout="@layout/Nav_header_main"/>