Java 膨胀类工具栏时出错
当我想使用自己的自定义工具栏而不是默认的系统工具栏时,我遇到了一些问题,现在我想在MainActivity上使用该工具栏。 我为工具栏创建了自己的xml资源文件,并将其包含在我的MainActivity布局文件中,但当我运行应用程序时,出现了一些错误 android.view.InflateException:二进制XML文件行#2:膨胀类android.support.v7.widget.toolbar时出错 我不知道发生了什么,我试图更改xml文件的设置,但仍然出现错误。 请师父帮帮我。 谢谢你 注意。这是app_bar.xml(工具栏资源文件)的我的代码Java 膨胀类工具栏时出错,java,android,xml,android-layout,Java,Android,Xml,Android Layout,当我想使用自己的自定义工具栏而不是默认的系统工具栏时,我遇到了一些问题,现在我想在MainActivity上使用该工具栏。 我为工具栏创建了自己的xml资源文件,并将其包含在我的MainActivity布局文件中,但当我运行应用程序时,出现了一些错误 android.view.InflateException:二进制XML文件行#2:膨胀类android.support.v7.widget.toolbar时出错 我不知道发生了什么,我试图更改xml文件的设置,但仍然出现错误。 请师父帮帮我。 谢
这是我的MainActivity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fafafa"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/Title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f325272f"
android:textStyle="bold" />
<Button
android:id="@+id/btnNewTrans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="73dp"
android:background="@drawable/custom_button"
android:text="@string/NewTransaction" />
<Button
android:id="@+id/btnViewCashflow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView"
android:background="@drawable/custom_button"
android:text="@string/ViewCashflow" />
<Button
android:id="@+id/btnAddCateg"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_marginBottom="73dp"
android:layout_toLeftOf="@+id/btnRate"
android:layout_toStartOf="@+id/btnRate"
android:background="@drawable/custom_button"
android:text="@string/AddCategory" />
<Button
android:id="@+id/btnRate"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnAddCateg"
android:layout_alignLeft="@+id/spnCategSelect"
android:layout_alignStart="@+id/spnCategSelect"
android:text="@string/RateUs" />
<Button
android:id="@+id/btnSetting"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btnRate"
android:layout_toEndOf="@+id/btnRate"
android:layout_toRightOf="@+id/btnRate"
android:text="@string/Setting" />
<Button
android:id="@+id/btnAbout"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btnSetting"
android:layout_toEndOf="@+id/btnSetting"
android:layout_toRightOf="@+id/btnSetting"
android:text="@string/About" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="@string/Trademark"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#f325272f"
android:textStyle="bold" />
<Spinner
android:id="@+id/spnCategSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnAddCateg"
android:layout_alignEnd="@+id/textView"
android:layout_alignLeft="@+id/btnViewCashflow"
android:layout_alignRight="@+id/textView"
android:layout_alignStart="@+id/btnViewCashflow"
android:spinnerMode="dropdown" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnNewTrans"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="@string/SelectCategory"
android:textAppearance="?android:attr/textAppearanceSmall" />
这是我的MainActivity.java
public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
private static Button BtnINewTrans;
private static Button BtnIViewCash;
private static Button BtnIAddCateg;
Spinner my_Spinner;
DatabaseHelper dbHelper = new DatabaseHelper(this);
public static String catSelected = null;
private Toolbar toolbar;
//ArrayAdapter<String> adapterCategory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
my_Spinner.setOnItemSelectedListener(this);
select_spinner_Category();
onButtonClickButtonListener();
}
/*ArrayList<String> my_array = new ArrayList<String>();
my_array = getTableValues();*/
/*ArrayAdapter my_Adapter = new ArrayAdapter(this, R.layout.spinner_row, my_array);
My_spinner.setAdapter(my_Adapter);*/
public void select_spinner_Category () {
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
DatabaseHelper dbH = new DatabaseHelper(getApplicationContext());
List<String> listCategory = dbH.getAllCategory();
ArrayAdapter<String> adapterCategory = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listCategory);
adapterCategory
.setDropDownViewResource(android.R.layout.simple_spinner_item);
my_Spinner.setAdapter(adapterCategory);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id){
String label = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "You selected "+label,
Toast.LENGTH_LONG).show();
catSelected = label;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public String getCatSelected(){
return catSelected;
}
/*ArrayList<String> arrayCategory;
arrayCategory = dbHelper.getAllCategory();
selectCategory = (Spinner) findViewById(R.id.spnCategSelect);
ArrayAdapter adapterCategory = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arrayCategory);
// adapterCategory = new ArrayList<String>(this, android.R.layout.simple_spinner_item, R.id.spnCategSelect, AllCategoryList);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectCategory.setAdapter(adapterCategory);
selectCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onButtonClickButtonListener(){
BtnINewTrans = (Button)findViewById(R.id.btnNewTrans);
BtnINewTrans.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentNewTrans = new Intent ("com.example.ever_ncn.cashflow.NewTransaction");
startActivity(intentNewTrans);
}
}
);
BtnIViewCash = (Button)findViewById(R.id.btnViewCashflow);
BtnIViewCash.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentViewCash = new Intent ("com.example.ever_ncn.cashflow.ViewCashflow");
startActivity(intentViewCash);
}
}
);
BtnIAddCateg = (Button)findViewById(R.id.btnAddCateg);
BtnIAddCateg.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.CategorySetting");
startActivity(intentAddCateg);
}
}
);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public类MainActivity扩展AppCompativeActivity实现OnItemSelectedListener{
专用静态按钮BtnINewTrans;
私人静态按钮BtnIViewCash;
专用静态按钮BtnIAddCateg;
旋转器我的旋转器;
DatabaseHelper dbHelper=新的DatabaseHelper(此);
公共静态字符串catSelected=null;
专用工具栏;
//阵列适配器类别;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
工具栏=(工具栏)findViewById(R.id.app\u栏);
设置支持操作栏(工具栏);
my_微调器=(微调器)findViewById(R.id.spnCategSelect);
my_Spinner.setOnItemSelectedListener(此);
选择_微调器_类别();
OnButtonClickButtoListener();
}
/*ArrayList my_array=新建ArrayList();
my_数组=getTableValues()*/
/*ArrayAdapter my_Adapter=新的ArrayAdapter(此,R.layout.spinner_行,my_数组);
My_spinner.setAdapter(My_Adapter)*/
公共无效选择\微调器\类别(){
my_微调器=(微调器)findViewById(R.id.spnCategSelect);
DatabaseHelper dbH=新的DatabaseHelper(getApplicationContext());
List listCategory=dbH.getAllCategory();
ArrayAdapter适配器类别=新的ArrayAdapter(此,
android.R.layout.simple\u微调器\u项目,列表类别);
自适应分类
.setDropDownViewResource(android.R.layout.simple\u微调器\u项);
my_Spinner.setAdapter(适配器类别);
}
@凌驾
已选择公共位置(AdapterView父对象、视图、整型位置、,
长id){
String label=parent.getItemAtPosition(position.toString();
Toast.makeText(parent.getContext(),“您选择的”+标签,
Toast.LENGTH_LONG).show();
catSelected=标签;
}
@凌驾
未选择公共无效(AdapterView父级){
}
公共字符串getCatSelected(){
返回所选的目录;
}
/*ArrayList arrayCategory;
arrayCategory=dbHelper.getAllCategory();
selectCategory=(微调器)findViewById(R.id.spnCategSelect);
ArrayAdapter adapterCategory=新的ArrayAdapter(这是android.R.layout.simple\u微调器项目,arrayCategory);
//adapterCategory=new ArrayList(这个,android.R.layout.simple\u微调器\u项,R.id.spnCategSelect,AllCategoryList);
adapterCategory.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
选择Category.setAdapter(适配器类别);
selectCategory.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position)+“selected”,Toast.LENGTH_LONG.show();
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
}*/
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
public void onButtonClickButtonListener(){
BtnINewTrans=(按钮)findviewbyd(R.id.btnNewTrans);
BtnINewTrans.setOnClickListener(
新建视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent intentNewTrans=新意图(“com.example.ever\n.cashflow.NewTransaction”);
起始触感(意向性新交易);
}
}
);
BtnIViewCash=(按钮)findViewById(R.id.btnViewCashflow);
BtnIViewCash.setOnClickListener(
新建视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent intentViewCash=新意图(“com.example.ever\n.cashflow.viewcash”);
StartViewCash;
}
}
);
BtnIAddCateg=(按钮)findViewById(R.id.btnAddCateg);
BtnIAddCateg.setOnClickListener(
新建视图。OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent intentAddCateg=新的Intent(“com.example.everncn.cashflow.CategorySetting”);
startActivity(意向分类);
}
}
);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}类是区分大小写的:你有
android.support.v7.widget.toolbar
,当它应该是android.support.v7.widget.toolbar哦,是的,你说得对,兄弟,这只是一个打字错误,谢谢你的回答。
public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
private static Button BtnINewTrans;
private static Button BtnIViewCash;
private static Button BtnIAddCateg;
Spinner my_Spinner;
DatabaseHelper dbHelper = new DatabaseHelper(this);
public static String catSelected = null;
private Toolbar toolbar;
//ArrayAdapter<String> adapterCategory;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
my_Spinner.setOnItemSelectedListener(this);
select_spinner_Category();
onButtonClickButtonListener();
}
/*ArrayList<String> my_array = new ArrayList<String>();
my_array = getTableValues();*/
/*ArrayAdapter my_Adapter = new ArrayAdapter(this, R.layout.spinner_row, my_array);
My_spinner.setAdapter(my_Adapter);*/
public void select_spinner_Category () {
my_Spinner = (Spinner)findViewById(R.id.spnCategSelect);
DatabaseHelper dbH = new DatabaseHelper(getApplicationContext());
List<String> listCategory = dbH.getAllCategory();
ArrayAdapter<String> adapterCategory = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, listCategory);
adapterCategory
.setDropDownViewResource(android.R.layout.simple_spinner_item);
my_Spinner.setAdapter(adapterCategory);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id){
String label = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "You selected "+label,
Toast.LENGTH_LONG).show();
catSelected = label;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public String getCatSelected(){
return catSelected;
}
/*ArrayList<String> arrayCategory;
arrayCategory = dbHelper.getAllCategory();
selectCategory = (Spinner) findViewById(R.id.spnCategSelect);
ArrayAdapter adapterCategory = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arrayCategory);
// adapterCategory = new ArrayList<String>(this, android.R.layout.simple_spinner_item, R.id.spnCategSelect, AllCategoryList);
adapterCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectCategory.setAdapter(adapterCategory);
selectCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onButtonClickButtonListener(){
BtnINewTrans = (Button)findViewById(R.id.btnNewTrans);
BtnINewTrans.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentNewTrans = new Intent ("com.example.ever_ncn.cashflow.NewTransaction");
startActivity(intentNewTrans);
}
}
);
BtnIViewCash = (Button)findViewById(R.id.btnViewCashflow);
BtnIViewCash.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentViewCash = new Intent ("com.example.ever_ncn.cashflow.ViewCashflow");
startActivity(intentViewCash);
}
}
);
BtnIAddCateg = (Button)findViewById(R.id.btnAddCateg);
BtnIAddCateg.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentAddCateg = new Intent ("com.example.ever_ncn.cashflow.CategorySetting");
startActivity(intentAddCateg);
}
}
);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}