Fragment 中未显示操作栏菜单 Activity

Action bar menu is not shown in Fragment Activity

我正在尝试在我的 Fragment 中显示我的操作栏菜单 Activity 问题是,如果我 select (在 android 工作室) Material 主题,看到操作栏菜单没有问题,但是当我自己测试我的应用程序时 phone (Samsung S5), 菜单未显示。

这是我的 Activity 的详细信息(我只展示了它的一部分,有必要):

package avappmobile.mytasks;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Calendar;
import java.util.Locale;

/**
 * Created by Alexandre on 18/02/2015.
 */
public class AddTask extends FragmentActivity implements DatePFragment.OnDatePickedListener, TimePFragment.OnTimePickedListener {

    // Variables to be used
    private EditText txtTaskName;
    private EditText txtTaskDate;
    private EditText txtTaskTime;

    private String taskName;
    private int taskyear;
    private int taskmonth;
    private int taskday;
    private int taskhour;
    private int taskminute;
    private String taskdate;
    private String tasktime;

    private Task mTask;
    private Calendar cal;

    private DatabaseHandler dbHandler;

    final Context context = this;

    private static final String FR_LANG_CONTEXT = "fr";
    private static final String US_LANG_CONTEXT = "en";

    FragmentManager fm = getSupportFragmentManager();

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

        // Locate the button and textView of the xml display and associate them to the private variables.
        txtTaskName = (EditText) findViewById(R.id.txtTaskName);
        txtTaskDate = (EditText) findViewById(R.id.txtTaskDate);
        txtTaskTime = (EditText) findViewById(R.id.txtTaskTime);
        cal = Calendar.getInstance();
        dbHandler = new DatabaseHandler(getApplicationContext());

        // As the softkeyboard seems to not hide automatically when the EditText Taskname lose focus, implementing the behavior
        txtTaskName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

                if (v.getId() == R.id.txtTaskName && !hasFocus){
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
        });

        // This prevent from having to tap twice to get the related onClick activity
        txtTaskDate.setFocusable(false);
        txtTaskTime.setFocusable(false);

        txtTaskDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                // Show Dialog Fragment
                showDatePickerDialog(v.getId());
            }
        });

        txtTaskTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                // Show Dialog Fragment
                showTimePickerDialog(v.getId());
            }
        });
    }

    @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_add_task, menu);
        return super.onCreateOptionsMenu(menu);
        //return true;
    }

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

我的 XML 菜单(通过我的 Activity 显示:menu_add_task.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=".AddTask">
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

最后是 AddTask 片段的 xml Activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="fill_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=".AddTask">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtTaskName"
        android:layout_gravity="center_horizontal"
        android:hint="@string/txtTaskName"
        android:gravity="center_horizontal"
        android:layout_above="@+id/txtTaskDate"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="20dp"
        android:focusable="true"
        android:focusableInTouchMode="true" />

    <EditText
        style="?android:attr/spinnerStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtTaskDate"
        android:layout_gravity="center_horizontal"
        android:hint="@string/txtTaskDate"
        android:gravity="center_horizontal"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true" />

    <EditText
        style="?android:attr/spinnerStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtTaskTime"
        android:layout_gravity="center_horizontal"
        android:hint="@string/txtTaskTime"
        android:gravity="center_horizontal"
        android:layout_below="@+id/txtTaskDate"
        android:layout_alignParentStart="true"
        android:layout_marginTop="20dp"
        android:clickable="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnCreateTask"
        android:id="@+id/btnCreateTask"
        android:layout_gravity="bottom|center"
        android:onClick="createNewTask"
        android:layout_alignParentBottom="true"
        android:layout_alignEnd="@+id/txtTaskTime"
        android:layout_marginEnd="40dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btnTaskList"
        android:id="@+id/btnTaskList"
        android:onClick="goToHomePage"
        android:layout_alignParentBottom="true"
        android:layout_toStartOf="@+id/btnCreateTask" />
</RelativeLayout>

如果您需要更多详细信息,请询问。

亚历克斯

尝试从 FragmentActivity 更改为 Activity

参考此 link 的 example

在测试了几件事之后,我最后的评论(关于我的问题)就是答案。

我不得不将 extends FragmentActivity 更改为 extends ActionBarActivity,这似乎是 FragmentActivity 的子类。

感谢您的帮助。

从 "FragmentActivity" 更改为 "AppCompatActivity" 完成工作并防止 "is deprecated" 警告。