我正在测试android中的Navigation Drawer示例项目,我在导航视图配置文件标题中设置文本时遇到问题。
这是我的代码:
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
TextView text = (TextView) findViewById(R.id.textView);
texto.setText("HELLO");
}
activity_main.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_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.DrawerLayout>
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: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">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@android:drawable/sym_def_app_icon" android:id="@+id/imageView" />
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing" android:text="Android Studio"
android:textAppearance="@style/TextAppearance.AppCompat.Body1" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="android.studio@android.com" android:id="@+id/textView" />
</LinearLayout>
当我尝试设置textview的文本时,我总是会收到此错误,并且它只发生在api level 23:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
如何从主要活动??
更改nav_header_main.xml的Textview文本提前致谢
答案 0 :(得分:54)
现在有了设计支持库的23.1.1版本,您可以使用
View header = navigationView.getHeaderView(0)
TextView text = (TextView) header.findViewById(R.id.textView);
答案 1 :(得分:45)
我遇到了同样的问题,并且可以使用此代码来避免它:
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_main, null);
navigationView.addHeaderView(header);
TextView text = (TextView) header.findViewById(R.id.textView);
text.setText("HELLO");
答案 2 :(得分:7)
Orium答案有效,这也可以。
View header = navigationView.inflateHeaderView(R.layout.nav_header_main);
TextView text = (TextView) header.findViewById(R.id.textView);
texto.setText("HELLO");
答案 3 :(得分:1)
这解决了我的问题
View headerView = navigationView.inflateHeaderView(R.layout.header_view);
txt_fullname = (TextView) headerView.findViewById(R.id.txt_fullname);
txt_email = (TextView) headerView.findViewById(R.id.txt_email);
答案 4 :(得分:0)
View headerView=navigationView.getChildAt(0);
TextView tvName=(TextView)hView.findViewById(R.id.name);
TextView tvEmail=(TextView)hView.findViewById(R.id.email);
tvName.setText(data);
tvEmail.setText(data);
答案 5 :(得分:0)
或者如果您使用此属性:
<android.support.desibn.widget.NavigationView>
...
app:headerLayout="@layout/your_header_layout"/>
你可以:
View header = mNavigationView.getHeaderView(0);
TextView title = (TextView) header.findViewById(R.id.your_title_id);
//Or something else
答案 6 :(得分:0)
以上答案是正确的,但在kotlin中,您需要使用findViewById<View>
val navigationHeader = navigation_view.getHeaderView(0)
val navigationTitle = navigationHeader.findViewById<View>(R.id.txt_navigation_title) as TextView
navigationTitle.text = "This is your header"