我正在尝试在片段活动中显示我的操作栏菜单 问题是,如果我选择(在Android工作室中)Material主题,看到操作栏菜单没有问题,但是当我在自己的手机(Samsung S5)上测试我的应用程序时,菜单没有显示。
以下是我的活动的详细信息(我只展示了它的一个子部分,有必要):
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 Fragment Activity的xml:
<?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>
如果您需要更多详细信息,请询问。
亚历。
答案 0 :(得分:5)
在测试了几件事之后,我的最后一条评论(在我的问题上)就是答案。
我必须将extends FragmentActivity
更改为extends ActionBarActivity
,这似乎是FragmentActivity的子类。
感谢您的帮助。
答案 1 :(得分:1)
从“FragmentActivity”更改为“AppCompatActivity”可以完成工作并防止“已弃用”警告。
答案 2 :(得分:0)
尝试从FragmentActivity
更改为Activity
。
请参阅此链接以查看其example