ActionBarSherlock with multiple MenuItems?

I used ABS 4.0 with two MenuItems in one of my applications, but found a small error: when I press the second MenuItem, it does the same as the first ...

I tried everything that I can think of, but it does not work. I changed onOptionItemSelected as I thought this was the method I needed to change.

EDIT:

I watched @Ollie's suggestions, but neither LogCat nor Debug show strange things. Maybe this is in some other part of the code or declaration for ABS? Here's the whole code, if you could view it, it would be great!

Code for all activities, how could it be in any other place?

package bas.sie.Antonius; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.SherlockActivity; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuItem; public class TeacherInfo extends SherlockActivity { String URLhome; String Info; String TeacherAb; TextView mTxtvInfo; Button mBtnTeacherStSchedule; Button mBtnTeacherDaySchedule; private static String mainUrl = "http://www.carmelcollegegouda.nl/site_ant/"; private static String endUrl = ".htm"; private static String[] myUrls = { "roosters/dagroosters/Doc_V1_", "roosters/standaardroosters/Doc1_" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contactinfo); setTitle("Over deze leraar"); ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); mTxtvInfo = (TextView) findViewById(R.id.TxtvTeacher); Intent startingIntent = getIntent(); Info = startingIntent.getStringExtra("contact"); mTxtvInfo.setText(Info); Intent startingIntent1 = getIntent(); TeacherAb = startingIntent1.getStringExtra("abbrev"); mBtnTeacherDaySchedule = (Button) findViewById(R.id.btnTeacherDaySchedule); mBtnTeacherStSchedule = (Button) findViewById(R.id.btnTeacherStSchedule); mBtnTeacherDaySchedule.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { URLhome = makeUrl(0); Intent i = new Intent(TeacherInfo.this, MyWebView.class); i.putExtra("home", URLhome); startActivityForResult(i, 0); } }); mBtnTeacherStSchedule.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { URLhome = makeUrl(1); Intent i = new Intent(TeacherInfo.this, MyWebView.class); i.putExtra("home", URLhome); startActivityForResult(i, 0); } }); } private String makeUrl(int index) { String s = mainUrl + myUrls[index] + TeacherAb + endUrl; return s; }// makeurl @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add("Instellingen") .setIcon(R.drawable.ic_settings) .setShowAsAction( MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); menu.add("Over de app") .setIcon(R.drawable.ic_about) .setShowAsAction( MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Intent intent = new Intent(this, AntoniusActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); return true; case R.id.settings: Intent i = new Intent(this, About.class); startActivity(i); return true; case R.id.about: Intent about = new Intent(this, About.class); startActivity(about); return true; default: return super.onOptionsItemSelected(item); } } } 

I think the problem is declaring menu items, but I don't see any problem there ...

Could you take a look at my menu.xml? Posted here:

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/settings" android:icon="@drawable/ic_settings" android:title="Instellingen"></item> <item android:id="@+id/about" android:icon="@drawable/ic_about" android:title="Over de app"></item> </menu> 
+3
source share
2 answers

Create the following menu:

 @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return super.onCreateOptionsMenu(menu); } 

Then use the switch statement to handle the selection:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // Do stuff return true; case R.id.menu_item_2: // Do stuff return true; default: return super.onOptionsItemSelected(item); } } 

EDIT . Finally, you have to do different things for each element, if you change the Intent's target activity to another, it will do what you expect:

 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: // ... Stuff ... case R.id.settings: // Settings item Intent i = new Intent(this, About.class); // Start About.java Activity, but item says "settings" // TODO: Change About to Settings? i = new Intent(this, Settings.class); startActivity(i); return true; case R.id.about: // About item Intent about = new Intent(this, About.class); // Start About.java Activty startActivity(about); return true; default: return super.onOptionsItemSelected(item); } } 
+7
source

What I find strange is how you create your menu. You determined the location of the menu in the menu.xml file, but you do not reference this layout in the onCreateOptionMenu () method. It should be something like this:

 @Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.menu, menu); return super.onCreateOptionsMenu(menu); } 

Note the getSupportMenuInflater () method, which is used instead of getMenuInflater (). Why should this be so, somewhere in the docemntation about the Android support library, which in terms is used by the ActionBarSherlock library.

What you do is create a menu in your code programmatically using the menu.add() method with the signature add(CharSequence) . Nowhere is there that you give ItemId. I assume that (and this is only an assumption) the android in this case assigns the same identifier to all elements, something like zero or some other arbitrary number. You should use the method with the signature add(int, int, int,CharSequence) or add(int, int, int, int) , since only those that allow you to specify ItemId. So, both elements of your menu have the same identifier. And it is (I think again) that they behave the same. Something else. Be careful that you use the correct replacement classes and methods from the support library and the ActionBarSherlock library. Please let us know if this solves the problem, as I only do it in my head.

+3
source

Source: https://habr.com/ru/post/921239/


All Articles