简体   繁体   English

扩展ListActivity时ActionBar无法正常工作

[英]ActionBar not working when extending ListActivity

I have a confusing problem. 我有一个令人困惑的问题。 I have a MainActivity with 2 actions : Update and Logout. 我有一个MainActivity,有2个动作:Update和Logout。 The problem is when I run the activity that extends ListActivity the action bar doesn't appear . 问题是当我运行扩展ListActivity的活动时, 操作栏不会出现 Below I have 2 images with 2 different extend types in MainActivity 下面我在MainActivity中有2个具有2种不同扩展类型的图像

Extending ActionBarActivity example 扩展ActionBarActivity示例

public class MainActivity extends ActionBarActivity

By extends ListActivity the result is the same as in the picture below. 通过扩展 ListActivity ,结果与下图中的相同。 Basically I want to make the main activity with a ListView and an a ction bar so that the user is able to update and logout using the action bar . 基本上我想用ListView和一个ction栏进行主要活动 ,以便用户能够使用操作栏进行更新注销 But it seems it doesn't work and i need your help . 但它似乎不起作用,我需要你的帮助 I tried searching on the web i couldn't find anything that helped. 我尝试在网上搜索我找不到任何有用的东西。

public class MainActivity extends ListActivity

Here you can see my manifest file: 在这里你可以看到我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.florin.statusapp" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-sdk android:minSdkVersion="11"
        android:targetSdkVersion="21"/>
    <application

        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
             >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <activity
            android:name=".RegisterActivity"
            android:label="@string/title_activity_register" >
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_login" >
        </activity>
        <activity
            android:name=".UpdateStatusActivity"
            android:label="@string/title_activity_update_status" >


        </activity>
    </application>

</manifest>

My MainActivity.java 我的MainActivity.java

public class MainActivity extends ListActivity{

private List<ParseObject> mStatusObjects;


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

    // Enable Local Datastore.
    Parse.initialize(this, "foo", "bar");


    ParseUser currentUser = ParseUser.getCurrentUser();
    if (currentUser != null) {



    } else {
        // show the login screen
        Intent toLoginActivity = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(toLoginActivity);

    }


}



@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;
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

@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
    switch (id) {
        case R.id.updateStatus:
            // take user to update activity
            Intent toMainActivityIntent = new Intent(MainActivity.this, UpdateStatusActivity.class);
            startActivity(toMainActivityIntent);
            break;

        case R.id.LogoutUser:
            //Log out user
            ParseUser.logOut();
            // take user to login activity
            Intent toLoginActivityIntent = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(toLoginActivityIntent);
            break;
    }

    return super.onOptionsItemSelected(item);
}

and the menu_main.xml for the action bar: 以及操作栏的menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.florin.statusapp.MainActivity">
    <item android:id="@+id/updateStatus"
        android:title="Update"
        app:showAsAction="always" />

    <item
        android:id="@+id/LogoutUser"
        android:title="Logout"
        app:showAsAction="never"

        />

</menu>

This should be related to your theme. 这应该与您的主题相关。 Action bars are only supported on themes after holo. 仅在holo之后的主题上支持操作栏。

http://developer.android.com/guide/topics/ui/actionbar.html#Adding http://developer.android.com/guide/topics/ui/actionbar.html#Adding

Your styles.xml probably has something like: 你的styles.xml可能有类似的东西:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

You can change it to this to use the holo theme: 您可以将其更改为使用holo主题:

<style name="AppTheme" parent="android:Theme.Holo">

As Tachyonflux said, on API 11 and higher, the action bar is included in all activities that use Theme.Holo or one of it's descendants 正如Tachyonflux所说,在API 11及更高版本中,操作栏包含在使用Theme.Holo或其中一个后代的所有活动中

Try adding the following to your AndroidManifest.xml 尝试将以下内容添加到AndroidManifest.xml中

<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Holo">

Or another Theme or your choosing. 或者另一个主题或您的选择。 Go to the link Tachyonflux has and look at the available options. 转到Tachyonflux链接并查看可用选项。 There are various default options, but you can also create your own. 有各种默认选项,但您也可以创建自己的默认选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM