【问题标题】:android app Toolbar.setTitle error Attemp to invoke virtual method 'void.androidx.appcompat.widget'android app Toolbar.setTitle错误尝试调用虚拟方法'void.androidx.appcompat.widget'
【发布时间】:2019-09-28 17:51:16
【问题描述】:

尝试使用此代码创建 android 简单应用程序:

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("mytitle");

还有这个

Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("mytitle");
setSupportActionBar(toolbar);

在Android X中都不能使用,请帮忙如何设置标题工具栏?

【问题讨论】:

  • 你能分享你的活动布局吗?

标签: java android toolbar


【解决方案1】:

我认为您导入了错误的工具栏类,或者说是 xml 和 java 代码中的两个不同的工具栏类

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("mytitle");

这段代码应该可以工作

【讨论】:

  • 这正是 OP 在问题中报告的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多