我正在尝试向我的activity_main.xml
添加菜单。我将menu.xml
添加到了res/menu
目录,并将其虚增为activity_main.xml
。
问题是它没有显示出来。我需要一些东西来触发onCreateOptionsMenu
方法吗?
menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/toast"
android:title="Raise Toast"
/>
</menu>
MainActivity.java
package com.nejat.lunchlist;
import android.app.DatePickerDialog;
import android.app.TabActivity;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Build;
import android.provider.MediaStore;
import android.support.annotation.IdRes;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.TimePicker;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
public class MainActivity extends TabActivity {
String[] autoAddr = {"mall", "foodcourt","school","places"};
ArrayList<Restaurant> model = new ArrayList<Restaurant>();
RestaurantAdapter adapter = null;
AutoCompleteTextView addr;
ArrayAdapter<String> adapter2 = null;
ListView listView;
private AutoCompleteTextView name = null;
private AutoCompleteTextView address= null;
private RadioGroup rButton;
TabHost tabHost;
DatePickerDialog datePickerDialog;
EditText datePickerEditText;
Restaurant r = new Restaurant();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.save);
datePickerEditText = (EditText) findViewById(R.id.date);
datePickerEditText.setOnClickListener(datePicker);
btn.setOnClickListener(onSave);
listView = (ListView) findViewById(R.id.restaurants);
adapter = new RestaurantAdapter(model,getApplicationContext());
listView.setAdapter(adapter);
listView.setOnItemClickListener(selectItem);
addr = (AutoCompleteTextView) findViewById(R.id.addr);
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,autoAddr);
addr.setThreshold(1);
addr.setAdapter(adapter2);
tabHost = (TabHost) findViewById(android.R.id.tabhost);
TabHost.TabSpec tabspec = tabHost.newTabSpec("List");
tabspec.setContent(R.id.restaurants);
tabspec.setIndicator("Restaurants");
tabHost.addTab(tabspec);
tabspec = getTabHost().newTabSpec("Details");
tabspec.setContent(R.id.details);
tabspec.setIndicator("Details");
tabHost.addTab(tabspec);
tabHost.setCurrentTab(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu,menu);
return true;
}
private View.OnClickListener datePicker = new View.OnClickListener(){
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int mHour = cal.get(Calendar.HOUR_OF_DAY);
int mminnute = cal.get(Calendar.MINUTE);
TimePickerDialog time = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
datePickerEditText.setText(hourOfDay + "/"+ minute);
r.setDate(hourOfDay + "/"+ minute);
}
},mHour,mminnute,false);
time.show();
}
};
private View.OnClickListener onSave = new View.OnClickListener() {
@Override
public void onClick(View v) {
AutoCompleteTextView name = (AutoCompleteTextView) findViewById(R.id.name);
EditText note = (EditText) findViewById(R.id.note);
RadioGroup rb = (RadioGroup) findViewById(R.id.types);
r.setAddress(addr.getText().toString());
r.setName(name.getText().toString());
r.setNote(note.getText().toString());
switch (rb.getCheckedRadioButtonId()){
case R.id.sit_down:
r.setType("Sit_Down");
break;
case R.id.take_out:
r.setType("Take_Out");
break;
case R.id.delivery:
r.setType("Delivery");
break;
}
adapter.add(r);
}
};
private AdapterView.OnItemClickListener selectItem = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Restaurant r= model.get(position);
name = (AutoCompleteTextView) findViewById(R.id.name);
address = (AutoCompleteTextView) findViewById(R.id.addr);
EditText note = (EditText) findViewById(R.id.note);
rButton = (RadioGroup) findViewById(R.id.types);
tabHost.setCurrentTab(1);
note.setText(r.getNote().toString());
name.setText(r.getName().toString());
address.setText(r.getAddress().toString());
if(r.getType().equals("Sit_Down")){
rButton.check(R.id.sit_down);
}
else if(r.getType().equals(("Delivery"))){
rButton.check((R.id.details));
}
else if(r.getType().equals("Take_Out")){
rButton.check(R.id.take_out);
}
}
};
}
ativity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/restaurants"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true" />
<TableLayout
android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:stretchColumns="1">
<TableRow>
<TextView android:text="Name:" />
<AutoCompleteTextView android:id="@+id/name" />
</TableRow>
<TableRow>
<TextView android:text="Address:" />
<AutoCompleteTextView android:id="@+id/addr" />
</TableRow>
<TableRow>
<TextView android:text="Date:" />
<EditText android:id="@+id/date"
android:clickable="true"
android:editable="false"/>
</TableRow>
<TableRow>
<TextView android:text="Type:" />
<RadioGroup android:id="@+id/types">
<RadioButton
android:id="@+id/take_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take-Out" />
<RadioButton
android:id="@+id/sit_down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sit-Down" />
<RadioButton
android:id="@+id/delivery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delivery" />
</RadioGroup>
</TableRow>
<TableRow>
<TextView android:text="Note:" />
<EditText android:id="@+id/note"/>
</TableRow>
<Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save" />
</TableLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
日志
12-07 03:56:24.774 1640-1665/system_process D/gralloc_ranchu: gralloc_unregister_buffer: exiting HostConnection (is buffer-handling thread)
12-07 03:56:29.761 2885-2890/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/metrics.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
12-07 03:56:29.764 2885-2890/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/help_responses.db.18' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
12-07 03:56:30.059 2885-2890/com.google.android.gms W/SQLiteConnectionPool: A SQLiteConnection object for database '/data/user/0/com.google.android.gms/databases/auto_complete_suggestions.db' was leaked! Please fix your application to end transactions in progress properly and to close the database when it is no longer needed.
答案 0 :(得分:0)
菜单没有显示在顶部的原因是因为我正在扩展TabActivity而不是AppCompatActivity。