为什么我的Android应用程序没有ActionBar?

时间:2015-09-23 00:27:20

标签: android android-activity android-actionbar

我有一些愚蠢的问题和问题。 我已经开发了一个Android应用程序并且它现在完成了60%,但我刚刚注意到我的MainActivity没有任何操作栏,但是另一个活动有它。 请高手,你能告诉我发生了什么吗?我想在我的MainActivity上使用actionBar / toolBar。 我试着编辑我的style.xml文件,但它仍然像那样.. NB。这是我的MainActivity.java代码

public class MainActivity extends Activity 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;

//ArrayAdapter<String> adapterCategory;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    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);
}

}

这是我的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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/Title"
    android:id="@+id/textView"
    android:textColor="#f325272f"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textStyle="bold" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/NewTransaction"
    android:id="@+id/btnNewTrans"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="69dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ViewCashflow"
    android:id="@+id/btnViewCashflow"
    android:layout_below="@+id/btnNewTrans"
    android:layout_alignLeft="@+id/textView"
    android:layout_alignStart="@+id/textView" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/AddCategory"
    android:id="@+id/btnAddCateg"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="94dp"
    android:layout_toStartOf="@+id/btnNewTrans" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/RateUs"
    android:id="@+id/btnRate"
    android:layout_alignBottom="@+id/btnAddCateg"
    android:layout_toRightOf="@+id/btnAddCateg"
    android:layout_toEndOf="@+id/btnAddCateg" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Setting"
    android:id="@+id/btnSetting"
    android:layout_alignTop="@+id/btnRate"
    android:layout_toRightOf="@+id/btnRate"
    android:layout_toEndOf="@+id/btnRate" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/About"
    android:id="@+id/btnAbout"
    android:layout_alignTop="@+id/btnSetting"
    android:layout_toRightOf="@+id/btnSetting"
    android:layout_toEndOf="@+id/btnSetting" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/Trademark"
    android:id="@+id/textView2"
    android:textColor="#f325272f"
    android:textStyle="bold"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spnCategSelect"
    android:spinnerMode="dropdown"
    android:layout_above="@+id/btnAddCateg"
    android:layout_alignLeft="@+id/btnViewCashflow"
    android:layout_alignStart="@+id/btnViewCashflow"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="@string/SelectCategory"
    android:id="@+id/textView3"
    android:layout_above="@+id/spnCategSelect"
    android:layout_centerHorizontal="true" />

这个用于我的Style.xml文件

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
</style>

<style name="list">
    <item name="colorPrimary">@color/primaryColor</item>
    <item name="colorPrimaryDark">@color/primaryColorDark</item>
    <item name="colorAccent">@color/accentColor</item>

    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_below">@+id/textView16</item>
    <item name="android:layout_centerHorizontal">true</item>
    <item name="android:layout_above">@+id/textView17</item>
</style>

这是关于评论的新问题,colors.xml来源

<resources>
    <color name ="primaryColor">#9C27B0</color> //purple
    <color name="primaryColorDark">#7B1FA2</color>
    <color name="accentColor">#FF5722</color>

</resources>

先谢谢

2 个答案:

答案 0 :(得分:0)

根据docs您的Activity必须延长AppCompatActivity

(文档说ActionBarActivity,但此后不推荐使用AppCompatActivity - 请参阅here。)

答案 1 :(得分:0)

您需要扩展AppCompatActivity而不是活动。

注意

ActionBarActivity 已弃用,因此请不要使用它并使用AppCompatActivity代替。建议不要使用已弃用的类,因为它可以随时删除或停止工作,也不建议使用开发人员不再支持它,您可能会错过将来的更新。