ActionBarSherlock with multiple MenuItems?

巧了我就是萌 提交于 2019-12-10 10:20:10

问题


I've been using ABS 4.0 with two MenuItems in one of my apps, but have discovered a little error: When pressing the second MenuItem, it does exactly the same as the first one...

I've tried just about everything I can think of, but it isn't working. I've altered onOptionItemSelected, as I thought that was the method I need to edit.

EDIT:

I've been looking at @Ollie's suggestions, but neither LogCat nor Debug is showing weird things. Maybe it's in some other part of the code, or a declaration for ABS? Here's the entire code, if you could look through it, that would be great!

The code for the whole Activity, as it's maybe in some 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'm thinking that the problem is in the declaration of the 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>

回答1:


Create the menu like this:

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

Then use a switch statement to handle selections:

@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 should do different things for each item, if you change the Intent target Activity to another, it'll 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);
    }
}



回答2:


What I find odd is the way you create your menu. You have defined menu layout it in a menu.xml, yet you do not reference this layout in a onCreateOptionMenu() method. It should be something like this:

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

Pay attention to the getSupportMenuInflater() method which is used instead of getMenuInflater(). Why this must be so is somewhere in docemntation about android support library which in term is used by ActionBarSherlock library.

What you do is create menu in code programmatically by using a method menu.add() with a signature add(CharSequence). Nowhere it is in there that you give ItemId. I guess (and this is only a guess) android in that case assigns the same id to all items, something like zero or some other arbitrary number. You should use a method with a signature add(int, int, int,CharSequence) or add(int, int, int, int) as only those allow you to specify ItemId. So, both of your menu items have the same id. And this is (I guess again) the cause that they behave the same. One more thing. Be careful that you use the correct substitute classes and methods from support library and ActionBarSherlock library. Please let us know if this solved the problem as I am only running this in my head.



来源:https://stackoverflow.com/questions/9774815/actionbarsherlock-with-multiple-menuitems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!