类工具栏Android错误

时间:2015-09-24 16:13:59

标签: java android xml android-layout

我有一些问题,当我想使用我自己的自定义工具栏而不是默认系统工具栏时,现在我想在我的MainActivity上使用该工具栏。 我已经为工具栏制作了我自己的xml资源文件,并且我将它包含在我的MainActivity布局文件中,但是当我运行应用程序时我得到了一些错误说

  

android.view.InflateException:二进制XML文件行#2:错误膨胀类android.support.v7.widget.toolbar

我不知道发生了什么,我试图改变我的xml文件的设置,但仍然出现错误。 请高手,帮帮我。 谢谢你。

NB。这是我的app_bar.xml代码(工具栏资源文件)

<android.support.v7.widget.toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor">

这个是我的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);
}

}

1 个答案:

答案 0 :(得分:0)

类是区分大小写的:当android.support.v7.widget.toolbar

时,android.support.v7.widget.Toolbar的小写't'为{0}