无法修改 NavigationView 中的 TextView
Posted
技术标签:
【中文标题】无法修改 NavigationView 中的 TextView【英文标题】:Cannot Modify the TextView in a NavigationView 【发布时间】:2022-01-14 02:51:39 【问题描述】:我正在尝试根据当前在 Firebase 中登录应用程序的用户更新 NavigationView 中的名称和电子邮件。这是我的代码(我使用 android Studio 中的模板作为 NavigationView):
public class MainApp2 extends AppCompatActivity
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainApp2Binding binding;
FirebaseAuth fAuth;
FirebaseUser user;
String email, name;
TextView mail, nam;
//View header= naviView.getHeaderView(0);
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
View naviView = LayoutInflater.from(this).inflate(R.layout.nav_header_main_app2, null);
/*NavigationView navigView = (NavigationView) findViewById(R.id.nav_view);
navigView.setNavigationItemSelectedListener(this);
navigView.addHeaderView(naviView);*/
fAuth=FirebaseAuth.getInstance();
user= fAuth.getCurrentUser();
binding = ActivityMainApp2Binding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarMainApp2.toolbar);
/* binding.appBarMainApp2.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 = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main_app2);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
mail=(TextView)naviView.findViewById(R.id.email_nav);
nam=drawer.findViewById(R.id.name_nav);
System.out.println("1."+user.getEmail());
System.out.println("2."+user.getDisplayName());
if(user!=null)
System.out.println(email=user.getEmail());
email=user.getEmail();
//name=user.getDisplayName().toString().trim();
mail.setText(email);
//nam.setText(name);
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_app2, menu);
return true;
@Override
public boolean onSupportNavigateUp()
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main_app2);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
我曾经得到空指针错误,但添加行后
View naviView = LayoutInflater.from(this).inflate(R.layout.nav_header_main_app2, null);
我没有收到错误,但文本也没有改变。我也试过
/*NavigationView navigView = (NavigationView) findViewById(R.id.nav_view);
navigView.setNavigationItemSelectedListener(this);
navigView.addHeaderView(naviView);*/
但我在 (this) 上遇到了错误。我是 Android Studio 的新手,我不知道如何在没有错误和文本实际更改的情况下解决这个问题。
【问题讨论】:
【参考方案1】:我设法解决了这个错误。我必须在绑定之后初始化 NavigationView,所以在这两行之后
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
我输入了我的代码,它可以正常工作。
【讨论】:
以上是关于无法修改 NavigationView 中的 TextView的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI中sheet弹出嵌在NavigationView中的子视图无法用presentationMode关闭(dismiss)弹出视图的解决
SwiftUI中sheet弹出嵌在NavigationView中的子视图无法用presentationMode关闭(dismiss)弹出视图的解决
NavigationView 中的 ListView 不滚动