I'm having trouble with using menu item on action bar using slidingmenu library
with actionbarsherlock
.
Anyone can help?
Here is the code
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
}
return onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
this is the menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/print"
android:title="@string/printItem"
android:icon="@drawable/ic_print"
android:showAsAction="always" />
<item
android:id="@+id/share"
android:title="@string/shareItem"
android:icon="@drawable/ic_action_share"
android:showAsAction="always"
android:actionProviderClass="android.widget.ShareActionProvider" />
My logcat says that I encountered runtime error at
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.menu, menu);
return true;
}
Don't really understand your question. This code works in my app.
in MainActivity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
break;
case R.id.print:
//your code
break;
case R.id.share:
//your code
break;
default:
return false;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
in Fragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_fragment,menu);
super.onCreateOptionsMenu(menu, inflater);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.