Java 如何添加工具栏?

Java 如何添加工具栏?,java,android,toolbar,Java,Android,Toolbar,在看了一些类似的问题之后,我找不到为什么我会有错误膨胀类android.support.v7.widget.Toolbar,以及如何修复它 首先,在我的main_activity.java中,我导入了在其他问题上找到的两条androidx语句,并为工具栏添加了两行,如图所示: package com.example.tp01_android; 导入androidx.annotation.RequiresApi; 导入androidx.appcompat.app.AlertDialog; 导入an

在看了一些类似的问题之后,我找不到为什么我会有
错误膨胀类android.support.v7.widget.Toolbar
,以及如何修复它

首先,在我的
main_activity.java
中,我导入了在其他问题上找到的两条androidx语句,并为工具栏添加了两行,如图所示:

package com.example.tp01_android;
导入androidx.annotation.RequiresApi;
导入androidx.appcompat.app.AlertDialog;
导入androidx.appcompat.app.appcompat活动;
导入androidx.appcompat.widget.Toolbar;
...
导入com.google.android.material.floatingactionbutton.floatingactionbutton;
导入java.util.ArrayList;
公共类MainActivity扩展了AppCompatActivity{
最终上下文=此;
private ArrayList itemslistArray=新ArrayList();
私有EditText newItemTextField;
专用阵列适配器simpleAdapter;
私人浮动按钮制造厂;
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//------------------------------------工具栏-------------------------------------------------
**工具栏myToolbar=(工具栏)findViewById(R.id.my_工具栏);
设置支持操作栏(myToolbar)**
}
...
现在,在我的
main_activity.xml
中,我添加了以下工具栏



我做错了什么,或者应该更改什么?

您的
工具栏导入错误。请使用
androidx
one而不是support lib版本。 在xml中,使用
androidx.appcompat.widget.Toolbar
而不是
android.support.v7.widget.Toolbar

有关更多信息,请参阅


您所指的工具尚未更新,正在使用旧的支持工件。请切换到工具栏的
androidx.*
版本,就像您对其他组件所做的那样。

您的
工具栏导入错误。请使用
androidx
版本,而不是支持库版本。 在xml中,使用
androidx.appcompat.widget.Toolbar
而不是
android.support.v7.widget.Toolbar

有关更多信息,请参阅


您所指的工具尚未更新,正在使用旧的支持工件。请切换到工具栏的
androidx.
版本,就像您对其他组件所做的那样。

使用
androidx.appcompat.widget.toolbar
而不是
main\u activity.xml中的
android.support.v7.widget.toolbar
如果主题为“@style/theme.appcompat.Light.NoActionBar”,则您的工具栏将不可见。请改为使用“@style/theme.appcompat.Light”或使用“setSupportActionBar(将工具栏传递到此处)”如果您的主题为“@style/theme.AppCompat.Light.NoActionBar”,则您的工具栏将不可见。请改用“@style/theme.AppCompat.Light”。或使用“setSupportActionBar(在此处传递您的工具栏);”哦,是的,确定指南过时了,谢谢您的输入!哦,是的,确定指南过时了,谢谢您的输入!