java 扩展 ListActivity 时 ActionBar 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28159021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:06:26  来源:igfitidea点击:

ActionBar not working when extending ListActivity

javaandroidlistviewandroid-actionbarandroid-manifest

提问by Florin Soldan

I have a confusing problem. I have a MainActivity with 2 actions : Update and Logout. The problem is when I run the activity that extends ListActivitythe action bar doesn't appear. Below I have 2 images with 2 different extend types in MainActivity

我有一个令人困惑的问题。我有一个带有 2 个操作的 MainActivity:更新和注销。问题是当我运行扩展ListActivity的活动时,不会出现操作栏。下面我在MainActivity 中有 2 个具有 2 种不同扩展类型的图像

Extending ActionBarActivity example

扩展 ActionBarActivity 示例

public class MainActivity extends ActionBarActivity

By extendsListActivitythe result is the same as in the picture below. Basically I want to make the main activitywith a ListViewand an action barso that the user is able to updateand logoutusing the action bar. 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.

通过扩展ListActivity结果与下图相同。基本上我想用ListView和操作制作主要活动,以便用户能够使用操作栏更新注销。但它似乎不起作用,我需要你的帮助。我尝试在网上搜索我找不到任何有用的东西。

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.xmlfor 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>

回答by tachyonflux

This should be related to your theme. Action bars are only supported on themes after 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:

您可以将其更改为使用全息主题:

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

回答by Lucas Crawford

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. There are various default options, but you can also create your own.

或其他主题或您的选择。转到 Tachyonflux 的链接并查看可用选项。有各种默认选项,但您也可以创建自己的选项。